简体   繁体   English

R ggplot:无法使用多面图更改y轴比例范围

[英]R ggplot : Can't change y-axis scale range with facetted plot

I have a simple problem but I can't figure out why it won't work -> I can't adjust the y scale range on my faceted bar plot: 我有一个简单的问题,但是我无法弄清楚为什么它不起作用->我无法在多面条形图上调整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))

I would like the y-axis to go upto 50 but using scale_y_continuous strangely does not work, producing this result: 我希望y轴上scale_y_continuous 50,但奇怪地使用scale_y_continuous无效,产生以下结果:

在此处输入图片说明

You need to add a limits argument in your scale_y_continuous : 您需要在scale_y_continuous添加一个limits参数:

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

Otherwise you just define the breaks position, not the axis values range. 否则,您仅定义中断位置,而不是轴值范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM