簡體   English   中英

R ggplot:無法使用多面圖更改y軸比例范圍

[英]R ggplot : Can't change y-axis scale range with facetted 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))

我希望y軸上scale_y_continuous 50,但奇怪地使用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))

否則,您僅定義中斷位置,而不是軸值范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM