The title is relatively self explanatory. I would like to know how ggplot decides its default breaks (and hence labels).
From the below code, it looks like the method is the same for each geom:
library(ggplot2)
ggplot(data=mtcars,mapping=aes(x=carb,y=hp,fill=as.factor(gear)))+
geom_bar(stat="identity",position="dodge")
ggplot(data=mtcars,mapping=aes(x=carb,y=hp,fill=as.factor(gear)))+
geom_point()
Any help would be greatly appreciated
I had the same question myself, and Google brought me to this SO question, so I thought I'd do a bit of digging.
Suppose we plot
which gives us the following plot, and we wish to know how the breaks for
mpg
(10, 15, ..., 35),cyl
(4, 5, ..., 8), andhp
(100, 150, ..., 300) are derived.Focusing on
mpg
we inspect the code forscale_y_continuous
and see that it callscontinuous_scale
. Then, calling up?continuous_scale
we see, under the description for thetrans
argument, thatThen, looking up
?scales::trans_new
, we see that the default value for thebreaks
argument isextended_breaks()
. Following the trail, we find thatscales::extended_breaks
callslabeling::extended(rng[1], rng[2], n, only.loose = FALSE, ...)
. Applying this to our data,which is what we observe in the plot. This raises the question of why, despite
we don't observe 50 and 350 in the legend. My understanding is that the answer is related to https://stackoverflow.com/a/13888731/6455166.