I have to plot data with very different range values. I am using the facet design of ggplot2 with the option facet_grid(variable ~ ., scales = "free")
. However, I would like to set the values of the breaks on the y axis such that for all variables the breaks are c(0, max(variable)/2, max(variable))
. I tried using scale_y_continuous
, but it did not work.
Reproducible example:
v1 <- sample(rnorm(100, 10, 1), 30)
v2 <- sample(rnorm(100, 20, 2), 30)
v3 <- sample(rnorm(100, 50, 5), 30)
fac1 <- factor(rep(rep(c("f1", "f2", "f3"), each = 10), 3))
library(reshape2)
library(ggplot2)
df1 <- melt(data.frame(fac1, v1, v2, v3))
ggplot(df1, aes(fac1, value, group = variable)) +
geom_point() +
facet_grid(variable ~ ., scales = "free") +
theme_bw()
How to Set Axis Breaks in ggplot2 (With Examples) You can use the following syntax to set the axis breaks for the y-axis and x-axis in ggplot2: #set breaks on y-axis scale_y_continuous (limits = c (0, 100), breaks = c (0, 50, 100)) #set breaks on y-axis scale_x_continuous (limits = c (0, 10), breaks = c (0, 2, 4, 6, 8, 10))
These functions take a vector as a parameter that has breakpoints. If we need multiple breakpoints we can add those too. Here is a ggplot2 scatter plot with x-axis break using scale_x_continuous () function. This function has a breaks parameter that takes a vector as input which has all the points of axis break as vector points.
Load the package scales to access break formatting functions. Note that, since ggplot2 v2.0.0, date and datetime scales now have date_breaks, date_minor_breaks and date_labels arguments so that you never need to use the long scales::date_breaks () or scales::date_format ().
If it is not, and you want to force the graph to display the Y axis from 0 to 100 (with breaks every 20) – for example to equalise the axes of multiple plots displayed side-by-side – add limits=c(0,100)like so: + scale_y_continuous(limits=c(0,100), breaks=seq(0,100, by = 20)) – rvrvrv Jul 9 '15 at 10:44
This draws the three separate plots, with the y breaks set to 0, 0.5*max(value), and max(value). The three plots are combined using the gtable rbind function, then drawn.
v1 <- sample(rnorm(100, 10, 1), 30)
v2 <- sample(rnorm(100, 20, 2), 30)
v3 <- sample(rnorm(100, 50, 5), 30)
fac1 <- factor(rep(rep(c("f1", "f2", "f3"), each = 10), 3))
library(reshape2)
library(ggplot2)
library(grid)
library(gridExtra)
df1 <- melt(data.frame(fac1, v1, v2, v3))
# Draw the three charts, each with a facet strip.
# But drop off the bottom margin material
dfp1 = subset(df1, variable == "v1")
p1 = ggplot(dfp1, aes(fac1, value, group = variable)) +
geom_point() +
facet_grid(variable ~ .) +
scale_y_continuous(limits = c(0, ceiling(max(dfp1$value))),
breaks = c(0, ceiling(max(dfp1$value))/2, ceiling(max(dfp1$value)))) +
theme_bw()
g1 = ggplotGrob(p1)
pos = g1$layout[grepl("xlab-b|axis-b", g1$layout$name), "t"]
g1 = g1[-pos, ]
# Drop off the bottom margin material
dfp2 = subset(df1, variable == "v2")
p2 = ggplot(dfp2, aes(fac1, value, group = variable)) +
geom_point() +
facet_grid(variable ~ .) +
scale_y_continuous(limits = c(0, ceiling(max(dfp2$value))),
breaks = c(0, ceiling(max(dfp2$value))/2, ceiling(max(dfp2$value)))) +
theme_bw()
g2 = ggplotGrob(p2)
g2 = g2[-pos,]
dfp3 = subset(df1, variable == "v3")
p3 = ggplot(dfp3, aes(fac1, value, group = variable)) +
geom_point() +
facet_grid(variable ~ .) +
scale_y_continuous(limits = c(0, ceiling(max(dfp3$value))),
breaks = c(0, ceiling(max(dfp3$value))/2, ceiling(max(dfp3$value)))) +
theme_bw()
g3 = ggplotGrob(p3)
# Combine the three gtables
g = rbind.gtable(g1, g2, size = "max")
g = rbind.gtable(g, g3, size = "max")
# Draw the plot
grid.newpage()
grid.draw(g)
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