0

Can someone explain why the code below doesn't cut the Y axis off? I am trying to cut off 0 to 50000 and only display values above that. I tried this based on this post

data(iris)
iris2<-iris[,1:4]*5000  

ggplot(data=iris2, aes(x=Sepal.Length, y=Sepal.Width, fill=Sepal.Length)) +
  coord_cartesian(ylim = c(5000, 150000)) +
  geom_bar(stat="identity")
Community
  • 1
  • 1
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • 1
    Because in bar graphs you compare the length of bars starting at 0. This is an instance where `ggplot2` is deliberately designed to make it difficult to make [a misleading plot](http://cloudfront.mediamatters.org/static/uploader/image/2014/03/31/obamacareenrollment-fncchart.jpg). – Gregor Thomas Feb 19 '17 at 20:36
  • After some experimentation with your example it seems the limits are used but in a loose way. If I limit to 100k it plots up to about 110. Similar with the lower number – tjjjohnson Feb 19 '17 at 20:56
  • I was amused (and slightly horrified) at what I saw with `+ylim ( 5000, 150000)+` – IRTFM Feb 19 '17 at 21:24
  • @Gregor, I agree, but I am struggling to show a small difference, In my specific case each value is above 5000 and may vary by a hundred or so. The problem is that you can't see the small change between bars – Rilcon42 Feb 20 '17 at 01:43
  • Right - it's a good indication that you shouldn't be using a bar plot. Just use a line plot or a scatter plot instead! – Gregor Thomas Feb 20 '17 at 16:51
  • Good point..... always a good idea to pick the right tool for the job – Rilcon42 Feb 27 '17 at 17:38

1 Answers1

0

Change your code to:

ggplot(data=iris2, aes(x=Sepal.Length, y=Sepal.Width, fill=Sepal.Length)) + ylim(c(5000, 150000)) +geom_bar(stat="identity")

you only need the ylim, the coord_cartesian is not necessary

Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54