Reputation: 11
I am using ggplot to plot y versus x for two factors f1 and f2 using the facet_wrap. I want to keep the ticks for the y axis only for the first column (representing given value of factor f2) and remove the others. Is there away to do this? I tried many ways (including scale = free_y) but no success. Below is a simple code:
y = rnorm(100)
x = rnorm(100)
type = rep(1:5,20)
f1 = sample(LETTERS[1:3], 100, replace=TRUE, prob=c(0.3, 0.3, 0.4) )
f2 = sample(LETTERS[4:6], 100, replace=TRUE, prob=c(0.3, 0.3, 0.4) )
df = data.frame(cbind(x, y,f1,f2, type))
df$x = as.numeric(as.character(df$x)); df$y = as.numeric(as.character(df$y))
p1 = ggplot(data = df, aes(x, y, linetype = type)) +
geom_line(aes(linetype = type))+ scale_linetype_manual(values=c("solid", "F1", "dotted", "twodash","dashed")) +
scale_size_manual(values=c(0.5, 0.5, 0.5,0.5,0.5)) +
geom_point(size=0.5, shape=21, fill="black") +
labs(y="y") +
facet_wrap( ~ f1 + f2 , ncol=3, scales = "free_y") +
theme_bw() +
theme(panel.margin = unit(0.8, "lines")) +
theme(plot.title = element_text(size = rel(1.2))) +
theme(axis.ticks.length=unit(0.2,"cm")) +
theme(strip.text.x = element_text(size=11)) +
theme(strip.background = element_rect(colour="white", fill="gray"))
p1
How to keep the ticks for the y axis only for the first column in the left (i.e factor f2 = "D"). I know the y axis have different levels but this is not an issue for me.
many thanks
Abderrahim
Upvotes: 1
Views: 4691
Reputation: 415
I think you are actually after facet_grid
as opposed to facet_wrap
.
See the below:
p1 <- ggplot(data = df, aes(x, y, linetype = type)) +
geom_line(aes(linetype = type))+ scale_linetype_manual(values=c("solid", "F1", "dotted", "twodash","dashed")) +
scale_size_manual(values=c(0.5, 0.5, 0.5,0.5,0.5)) +
geom_point(size=0.5, shape=21, fill="black") +
labs(y="y") +
facet_grid( f1~f2 ) +
theme_bw() +
theme(panel.margin = unit(0.8, "lines")) +
theme(plot.title = element_text(size = rel(1.2))) +
theme(axis.ticks.length=unit(0.2,"cm")) +
theme(strip.text.x = element_text(size=11)) +
theme(strip.background = element_rect(colour="white", fill="gray"))
p1
Upvotes: 1