I have reading all post about scaling the y-axis and in all of them there are some extra steps in order to control the y-axis limits when using sec.axis in ggplot.
I have the following df.
structure(list(day = c(1, 3, 5, 7, 9), mean = c(0.000452620431539136,
0.000244953967091816, 0.000409529176828165, 0.000621566432113383,
0.000975471413145951), sd = c(0.000145928952108396, 7.48403498938327e-05,
8.70694523628839e-05, 0.000265199022927143, 0.00076194983870935
), group = c("pi", "pi", "pi", "pi", "pi")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
and
structure(list(day = c(1, 3, 5, 7), mean = c(NaN, 5.85880255563636,
4.16535426125, 3.22060147866667), sd = c(NaN, 0.363838291664683,
0.980379999667707, 1.17101416465057), group = c("Equi", "Equi",
"Equi", "Equi")), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
I run the following code:
-ggplot(data=DI.pi.sum, aes(x=day, y=mean)) + geom_bar(stat = "identity", fill = "grey", size = 1.5) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), size = 0.1, width=.3,
position=position_dodge(1)) +
geom_line(data=DI.Equi.sum, aes(x=day, y=mean/10000)) +
geom_ribbon(data = DI.Equi.sum,
aes(x=day, y = mean/10000, ymin=mean/10000-sd/10000, ymax=mean/10000+sd/10000),
alpha=0.2, fill = "grey40") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(face = "bold", size = 7),
axis.title.y = element_text(face = "bold", size = 10),
legend.direction = "vertical", legend.box = "horizontal") +
scale_size(range = c(5, 15)) +
scale_x_continuous(breaks = c(1, 3, 5, 7, 9), limits = c(0,10)) +
scale_y_continuous(limits=c(0, 0.0020), sec.axis = sec_axis(~ . * 10000), name = "pi")
Producing the following plot:
I do not need the sec axis to go up to 40. I would be great to get it to just 20.
Suggestions?
Thanks in advance.