use geom_rect but still see backround grid?

929 views
Skip to first unread message

squid

unread,
May 8, 2009, 5:55:24 AM5/8/09
to ggplot2
Hi,
I can't find my old post from Tue, Apr 28, 2009 at 8:39 PM
subject Re: split plot and vertical shaded background

in which Hadley suggested I try geom_rect to put some background
shading in particular rectangular regions of my plot. I've been
playing with it and have discovered it is important to plot the
rectangles BEFORE the data if you want the data to show up on top of
the rectangles. This makes sense in the 'layers' philosophy, but took
me a minute to figure out.

Now I'm struggling with trying to find a way in which the shaded
rectangle is transparent to the background grids as well. My x axis
runs from 0 to 60million. I need background shading in two places on
that plot (see geom_rect below). This code works well but I can no
longer see the grid behind denoting the breaks on the x and y axes.


ggplot(data=subset(all, (Arm=="2R") &(x.m.cov >20000)&(x.s.cov
>20000)) , aes(block)) +
geom_rect(aes(xmin=18930000,xmax=26764357, ymin= 0, ymax=max
(ms.x.shared) ), fill="grey80")+
geom_rect(aes(xmin=26764357,xmax=31450000, ymin= 0, ymax=max
(ms.x.shared) ), fill="grey70")+
geom_line(aes(y = ms.x.shared, colour = "Shared polymorphisms\n
between M and S\n"), size= .75) +
geom_line(aes(y = ms.x.fixed, colour = "Fixed differences\n between M
and S\n"), size= .75) +
labs(x = "2R (in Mb)", y = "Proportion of sites") + opts
(axis.title.x=theme_text(size=14), axis.title.y=theme_text
(size=14,angle = 90))+
opts(title = "Polymorphisms and fixed differences in 50kb windows,
minimum coverage per window=20kb") + opts(plot.title = theme_text
(size=16)) +
scale_colour_discrete("Site type") +
scale_x_continuous(breaks=c
(0,10000000,20000000,30000000,40000000,50000000,60000000), labels=c
("0\nTELOMERIC", "10\n", "20\n","30\n", "40\n", "50\n",
"60\nCENTROMERIC")) +
scale_colour_brewer(palette="Dark2") +
opts(axis.text.x = theme_text(colour = "black", vjust=1))+
opts(axis.text.y = theme_text(colour = "black", hjust=1))


Is there something like a 'transparency' function for geom_rect so
that I might still be able to see the horizontal and vertical white
lines that grid the background of all plots?

Additionally, is there something like "free_y" for the ymax entry? I
have chosen the max of my data which is easy here with only two
variables plotted, but if I had ten variables, I'd have to figure out
which had the maximum value for each plot.

Thanks, again.

squid

unread,
May 8, 2009, 12:41:59 PM5/8/09
to ggplot2
Alternatively, is there a way to turn OFF the background grid? I'm
using theme_bw() if it matters.
Thanks.

squid

unread,
May 13, 2009, 7:09:07 AM5/13/09
to ggplot2
Hi. No suggestions regarding how to turn off the background grid have
been put forth, so instead, I've been covering it up with a white
rectangle, using geom_rect. This seems to lead to incredibly heavy
output that when I print to pdf, causes Preview (Mac pdf reader) to
choke.

Is there another option to remove the background grid?
Thanks.

baptiste auguie

unread,
May 13, 2009, 7:15:43 AM5/13/09
to squid, ggplot2
I don't understand your problem: can you post a minimal, reproducible example?

Also, try this:

p <- qplot(mpg, wt, data=mtcars, facets=vs ~ am)
p + opts(panel.grid.major=theme_line(colour = NA, size = 0.0),
panel.grid.minor=theme_line(colour = NA, size = 0.0))


HTH,

baptiste
--

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________
Reply all
Reply to author
Forward
0 new messages