eliminating x-axis ticks but keeping the labels???

219 views
Skip to first unread message

Shai

unread,
Dec 23, 2010, 3:47:26 AM12/23/10
to ggplot2
Hi All!

I'm plotting a bar graph and would like to get rid of the tixks on the
x-axis. In a previous thread Hadley suggested to set the breaks to NA:

http://groups.google.com/group/ggplot2/browse_thread/thread/f60879c9b980af9f/0ea252285ba64942?lnk=gst&q=x+axis+ticks#0ea252285ba64942

but this eliminates the labels as well! Any suggestions?


dat <- data.frame(cat=rep(c('Site1', 'Site2'), each=2), sex=rep(c('F',
'M'), each=1), dv=abs(rnorm(4, mean=10,sd=2)))
none <- theme_blank()
dodge <- position_dodge(width=0.9)
qplot(data=dat, x=factor(sex), y = dv)
p <- ggplot(dat, aes(x=factor(cat), y = dv, fill=sex))
p
+geom_bar(stat='identity',position="dodge",size=0.5)+scale_x_discrete(breaks=NA)


Thanks
Shai

Brandon Hurr

unread,
Dec 23, 2010, 4:36:37 AM12/23/10
to Shai, ggplot2
last_plot() + +opts(axis.x.ticks = theme_blank())

will get rid of all axis ticks, but I'm not sure how do only eliminate the x-axis ones. 

B


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Dec 23, 2010, 4:37:28 AM12/23/10
to Shai, ggplot2
Oops, extra plus in there...

darn copy pasta.

On Thu, Dec 23, 2010 at 09:36, Brandon Hurr <brando...@gmail.com> wrote:
last_plot() + opts(axis.x.ticks = theme_blank())

Brandon Hurr

unread,
Dec 23, 2010, 4:38:50 AM12/23/10
to Shai, ggplot2
I'm bad this morning. axis.x.ticks doesn't do anything. 

On Thu, Dec 23, 2010 at 09:37, Brandon Hurr <brando...@gmail.com> wrote:
last_plot() + opts(axis.ticks = theme_blank())

Dennis Murphy

unread,
Dec 23, 2010, 10:52:52 AM12/23/10
to Shai, ggplot2
Hi:

This gets rid of the ticks on both the x- and y- axis. I'll let you decide whether or not it's acceptable.

... + opts(axis.ticks = theme_blank())

As far as I can tell from the book documentation, it's not possible to remove the ticks on one axis at present...but perhaps someone will show you that/how it can be done. (I'm aware of ongoing work to separate tasks, so it may well be available in the foreseeable future.) Lattice and base graphics allow this type of separation of tasks, however, if it's that important.

HTH,
Dennis

Kohske Takahashi

unread,
Dec 23, 2010, 11:11:58 AM12/23/10
to Dennis Murphy, Shai, ggplot2
Hi, here is a bad hack:

p <- qplot(mpg, wt, data=mtcars, facets = vs ~ cyl)
grid.gremove(gPath("axis_h", "axis.ticks.segments"), grep=T)

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Shai Pilosof

unread,
Dec 23, 2010, 11:49:09 AM12/23/10
to Kohske Takahashi, Dennis Murphy, ggplot2
Wow, it actually works!
Although when you save it with ggsave("C:/Figure.png") the ticks do appear... :(

Thanks for the help :)
Shai

Brandon Hurr

unread,
Dec 23, 2010, 2:23:34 PM12/23/10
to Shai Pilosof, Kohske Takahashi, Dennis Murphy, ggplot2
I think ggsave() replots the contents of last_plot() so it won't save the grid.remove() edits. 

Brandon Hurr

unread,
Dec 23, 2010, 2:31:07 PM12/23/10
to Shai Pilosof, Kohske Takahashi, Dennis Murphy, ggplot2
You can save it the old fashion way.

png("graph.png") #or whatever device you want to use
p+geom_bar(stat='identity',position="dodge",size=0.5)
grid.gremove(gPath("axis_h", "axis.ticks.segments"), grep=T)
dev.off()
graph.png
Reply all
Reply to author
Forward
0 new messages