2017-02-13 259 views
0

任何人都可以幫助我將圖中水平軸的極限值設置爲(0,1)嗎?下面的代碼不起作用。調整棒圖中的軸限制ggplot2

set.seed(234) 
data <- data.frame(var1 = c(rep('A',3),rep('B',3)), 
        var2 = runif(6), 
        var3 = rep(c('x1','x2','x3'),2)) 

ggplot(data,aes(x=var1,y=var2,fill=factor(var3))) + 
    geom_bar(stat="identity",position="dodge") + 
    scale_y_continuous(breaks=c(0,0.5,1.0)) + 
    coord_cartesian(ylim=c(0,1.0)) + 
    coord_flip() 

enter image description here

回答

2

無需coord_cartesian(),您可以()直接傳遞ylim ARG到coord_flip。

set.seed(234) 
library(ggplot2) 
data <- data.frame(var1 = c(rep('A',3),rep('B',3)), 
       var2 = runif(6), 
       var3 = rep(c('x1','x2','x3'),2)) 

ggplot(data,aes(x=var1,y=var2,fill=factor(var3))) + 
    geom_bar(stat="identity",position="dodge") + 
    scale_y_continuous(breaks=c(0,0.5,1.0)) + 
    coord_flip(ylim = c(0, 1)) 

enter image description here

http://docs.ggplot2.org/0.9.3.1/coord_flip.html