is it possible to manually add breaks and labels to the secondary y-axis using ggplot2?
(see bottom right)
I want more compact breaks on the right y-axis, representing the bars.
ggplot2 dual axes supportscale_x_continuous() and scale_y_continuous() can now display a secondary axis that is a one-to-one transformation of the primary axis (e.g. degrees Celcius to degrees Fahrenheit). The secondary axis will be positioned opposite to the primary axis and can be controlled with the sec.
Methodn1: Break Y-Axis of Plot Using gap. plot() Function of plotrix Package. In this method break y-axis of the plot using the gap. plot() Function of plotrix Package, the user first needs to install and import the plotrix package to the working console of the R, further the user needs to call the gap.
To add axis breaks in ggplot2 plots in R, we use scale_x_break() and scale_y_break() functions. These functions take a vector as a parameter that has breakpoints. If we need multiple breakpoints we can add those too.
This graph will be the base case, then I'll show how to change breaks and labels on the secondary y-axis:
sapply(c("pipeR", "ggplot2"), require, character.only = TRUE)
data(swiss)
swiss %>>% ggplot() +
geom_bar(mapping = aes(x = Agriculture, y = Fertility * 30 / 400), stat = "identity", colour = gray(0.5), fill = gray(0.5)) +
geom_line(mapping = aes(x = Agriculture, y = Education)) +
geom_point(mapping = aes(x = Agriculture, y = Education), size = 3, shape = 21, fill = "white") +
scale_x_continuous() +
scale_y_continuous(
name = expression("Education"),
sec.axis = sec_axis(~ . * 400 / 30 , name = "Fertility"),
limits = c(0, 30)) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
Change the breaks:
swiss %>>% ggplot() +
geom_bar(mapping = aes(x = Agriculture, y = Fertility * 30 / 400), stat = "identity", colour = gray(0.5), fill = gray(0.5)) +
geom_line(mapping = aes(x = Agriculture, y = Education)) +
geom_point(mapping = aes(x = Agriculture, y = Education), size = 3, shape = 21, fill = "white") +
scale_x_continuous() +
scale_y_continuous(
name = expression("Education"),
sec.axis = sec_axis(~ . * 400 / 30 , name = "Fertility", breaks = seq(1,1000,10)),
limits = c(0, 30)) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
Change labels:
swiss %>>% ggplot() +
geom_bar(mapping = aes(x = Agriculture, y = Fertility * 30 / 400), stat = "identity", colour = gray(0.5), fill = gray(0.5)) +
geom_line(mapping = aes(x = Agriculture, y = Education)) +
geom_point(mapping = aes(x = Agriculture, y = Education), size = 3, shape = 21, fill = "white") +
scale_x_continuous() +
scale_y_continuous(
name = expression("Education"),
sec.axis = sec_axis(~ . * 400 / 30 , name = "Fertility", breaks = seq(1,1000,10), labels=rep("x",length(seq(1,1000,10)))),
limits = c(0, 30)) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
Useful link: https://whatalnk.github.io/r-tips/ggplot2-secondary-y-axis.nb.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With