R ggplot:无法使用刻面图改变y轴刻度范围

时间:2015-06-05 10:07:14

标签: r ggplot2 facet

我有一个简单的问题,但我无法弄清楚为什么它不起作用 - >我无法调整刻面条形图上的y刻度范围:

# Data #

df<-as.data.frame(c("x","y","z","x","y","z","x","y","z","x","y","z"))
colnames(df)<-"x"
df$y<-c(10,15,20,5,25,45,10,10,20,40,20,5)
df$facet<-c(1,1,1,1,1,1,2,2,2,2,2,2)
df$group<-c("A","A","A","B","B","B","A","A","A","B","B","B")

# Plot #

ggplot(df, aes(x=x, y=y, fill=group)) + 
  facet_grid( ~ facet) +
  scale_fill_manual(values=c("blue", "red")) +
  geom_bar(position="dodge", stat="identity") +
  theme(strip.text = element_text(face="bold", size=rel(1)),
        strip.background = element_rect(fill="white", colour="white", size=1)) +
  theme(panel.margin = unit(1, "lines")) +
  scale_x_discrete(expand = c(0, 0)) +
  theme(panel.grid.major.x = element_blank()) + theme(axis.ticks.x = element_blank()) +
  theme(legend.background=element_blank()) +
  scale_y_continuous("%", breaks=seq(0, 50, 10), minor_breaks=seq(0,50,5), expand = c(0, 0))

我希望y轴达到50但奇怪地使用scale_y_continuous不起作用,产生这个结果:

enter image description here

1 个答案:

答案 0 :(得分:4)

您需要在limits中添加scale_y_continuous参数:

scale_y_continuous("%", limits=c(0,50), breaks=seq(0, 50, 10), minor_breaks=seq(0,50,5), expand = c(0, 0))

否则,您只需定义中断位置,而不是轴值范围。