Reputation: 3263
I would like to specify the breaks and colors for two facets independently. Is there a way to do this?
Using this code
ggplot(data, aes(x = x, y = y)) +
geom_point() +
scale_y_continuous(breaks = c(0:3, 4, 6, 8)) +
facet_wrap(~group, scales = "free")
I can get add the additional breaks I need on the bottom facet, but I don't want these for the top. In other words, I'd like to have 0, 1, 2, 3
for the bottom plot and 0, 2, 4, 6, 8
for the top plot.
I don't have any idea of how to specify different colors for the labels between facets (let me know if I should open a separate question about the colors).
Upvotes: 0
Views: 1064
Reputation: 93871
One way to do this is to plot each group separately, then lay out the plots together:
library(gridExtra)
p1 = ggplot(data[data$group=="Group1",], aes(x = x, y = y)) +
geom_point() +
scale_y_continuous(breaks = c(0:3, 4, 6, 8)) +
facet_wrap(~ group)
p2 = ggplot(data[data$group=="Group2",], aes(x = x, y = y)) +
geom_point() +
scale_y_continuous(breaks = c(-1:3)) +
facet_wrap(~ group)
grid.arrange(p1, p2, ncol=1)
If you can come up with a rule for programmatically deciding the y-breaks, then you can do all the plots at once using lapply
. Here's a simple example. For your use case, you'll need to see if you can come up with a rule or rules that work for all the plots.
# Store all the plots in a list
pl = list()
pl = lapply(unique(data$group), function(i) {
ggplot(data[data$group==i,], aes(x,y)) +
geom_point() +
facet_wrap(~ group) +
scale_y_continuous(limits=c(min(data$y[data$group==i]), max(data$y[data$group==i])), breaks=min(data$y):max(data$y))
})
do.call(grid.arrange, c(pl, ncol=1))
Upvotes: 3