简体   繁体   中英

Adjust axis limit in bar plot ggplot2

Can anyone help me to set the limits of the horizontal axis in the figure to (0,1) please? The code below does not work.

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()

在此处输入图片说明

No need for coord_cartesian(), you can pass the ylim arg into coord_flip() directly.

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))

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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