slabofguinness
slabofguinness

Reputation: 821

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

enter image description here

Upvotes: 2

Views: 1680

Answers (1)

amallik
amallik

Reputation: 71

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

enter image description here

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

Upvotes: 3

Related Questions