Vinterwoo
Vinterwoo

Reputation: 3941

R and ggplot- Removing the "0" tick mark on the y axis

When plotting in R using ggplot, I've noticed that sometimes if you don't specify any limitations on the y-axis by default the plot will not have any "0" mark at the bottom of the y axis (it is assumed the bottom corner represents 0). The first plot on this page is a nice example

http://wiki.stdout.org/rcookbook/Graphs/Axes%20%28ggplot2%29

You can see that the bottom corner is left blank. This is what I would like.

But if I specify the limits of the y axis the 0 is always displayed. So if I use either

scale_y_continuous(limits=c(0,8)

or

ylim(0,8)

I get that little 0 and hash mark

So if I have a data set like:

ByYear <- data.frame( V1 = c(2005,2006,2007,2008,2005,2006,2008,2006,2007,2005,2006,2007,2008),
+                       V2 = c(0,0.2,0,1.6,2,5,0,4,3,0,8,0,5), 
+                       V3 = c('A','A','A','A','B','B','B','C','C','D','D','D','D'))

And run a basic plot like

ggplot(data=ByYear,aes(x=V1,y=V2,group=V3))+geom_line()+geom_point(aes(shape=V3),size=3)+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())

Is there a way I can have the first hash mark be blank? Something akin to

ylim(,8)  #(Even though it does not work)

I know I can use

expand=c(0,0)) or yaxis="i"

To bring the 0 mark down to the lower corner (which is better), but the only problem is because I have a lot of zero data this will cut off the bottom of the point shapes so I still need that little bit of buffer space below the zero point.

Upvotes: 1

Views: 3470

Answers (1)

joran
joran

Reputation: 173577

I think you may be looking for the breaks argument of scale_y_continuous:

ggplot(data=ByYear,aes(x=V1,y=V2,group=V3)) + 
    geom_line() + 
    geom_point(aes(shape=V3),size=3) + 
    scale_y_continuous(breaks = 1:8) +
    opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())

which produces this:

enter image description here

Note: Since version 0.9.2 opts has been replaced by theme:

+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Upvotes: 6

Related Questions