Specifying labels per facet manually?

2,404 views
Skip to first unread message

Sebastian Weber

unread,
Apr 5, 2011, 6:20:05 AM4/5/11
to ggplot2
Dear ggplot community,

I have the problem that labels which go along with a freely scaled axis within a facet are troubled by overplotting:

no <- 100
dd <- data.frame(xv=c(rnorm(no),rnorm(no,0,300) ), set=c(rep(1,no), rep(2,no)))

pdf("over.pdf", width=3.2,height=2.0)
ggplot(data=dd, aes(xv)) + geom_density() + facet_grid(.~set, scales="free_x")
dev.off()

How can I fix this, i.e. how to specify the labels for each facet manually? Increasing the plotting region is not an option.

Thank you very much for your help.

Best regards,

Sebastian

Dennis Murphy

unread,
Apr 5, 2011, 7:18:04 AM4/5/11
to Sebastian Weber, ggplot2
Hi:

Try this:

ggplot(data=dd, aes(xv)) + geom_density() + facet_wrap(~set, scales="free")

HTH,
Dennis

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

Sebastian Weber

unread,
Apr 5, 2011, 7:41:00 AM4/5/11
to Dennis Murphy, ggplot2
Hi!

The problem I have is not due to wrongly scaled y-axis, but rather the overplotting of x-axis labels due to the small plot size, i.e. I would like to say facet 1 should have labels set1=c(-2,0,2) and facet 2 should have label set2=c(-200, 0, 200) for example.

Best regards,

Sebastian

--
Sebastian Weber
Group of Cell Communication and Control
Freiburg Institute for Advanced Studies - FRIAS
School of Life Sciences - LIFENET
Albert-Ludwigs-Universität Freiburg
Albertstr. 19
79104 Freiburg
T.: +49-761-203-97237
Fax:+49-761-203-97334

Dennis Murphy

unread,
Apr 5, 2011, 8:20:12 AM4/5/11
to Sebastian Weber, ggplot2
Hi:

Here's one approach:

g1 <- ggplot(dd, aes(x = xv))
g2 <- g1 + geom_density(data = subset(dd, set == 1)) +
                 scale_x_continuous(breaks = c(-2, 0, 2))
g3 <- g1 + geom_density(data = subset(dd, set == 2)) +
                 scale_x_continuous(breaks = c(-600, 0, 600))
library(gridExtra)
grid.arrange(g2, g3, nrow = 1)

HTH,
Dennis

Sebastian Weber

unread,
Apr 5, 2011, 9:02:40 AM4/5/11
to Dennis Murphy, ggplot2
Hi!

This is not really a solution as the x-axis title is shared between the plots when using facets. This is important to me in order to maintain a consistent look with other plots I use in the document I am writing. If worst comes to worst, I would hard code some ggplot functions myself, but which ones I have to alter then?

Thanks a lot in advance,

Sebastian

baptiste auguie

unread,
Apr 5, 2011, 4:39:07 PM4/5/11
to Sebastian Weber, ggplot2
Hi,

You could perhaps tweak grid.pretty(),

grid.pretty <- function(range, ...)
if(abs(diff(range)) > 100) pretty(range, n=3, min.n =3) else
grid::grid.pretty(range, ...)

last_plot()

I suggested recently that ggplot2 could use a different alias
defaulting to grid.pretty(), so that it could be overwritten more
cleanly with user-defined functions. Anyone would like to comment on
that idea?

baptiste


On Wed, Apr 6, 2011 at 1:02 AM, Sebastian Weber

Sebastian Weber

unread,
Apr 6, 2011, 3:39:24 AM4/6/11
to baptiste auguie, ggplot2
Hi!

Thank you so much! This solved it really elegantly!

And yes, the idea to have the possibility to override grid.pretty is great. I would even go further: Allow users to override things depending on the facet, i.e. provide a function which gets called with a facet number or something like this...

Thanks again,

Sebastian

Reply all
Reply to author
Forward
0 new messages