Re: How to get rid of default grey background color in a lineplot legend

6,383 views
Skip to first unread message

Johannes Björk

unread,
Oct 11, 2012, 1:20:28 PM10/11/12
to ggplot2
Hi,

for me, this worked...

#removing background

p + theme(
+ panel.background = element_rect(fill = "transparent",colour =
NA), # or theme_blank()
+ panel.grid.minor = element_blank(),
+ panel.grid.major = element_blank(),
+ plot.background = element_rect(fill = "transparent",colour =
NA)) + xlim=c(1,12)

Cheers
Jo

On 11 oct, 16:07, Jannemann <janheusch...@gmail.com> wrote:
> Hello together,
> I was looking all over the place but I can't figure out how to get rid of
> the default grey background color in a lineplot legend. I tried to set the
> plot and the panel background to blank but with no success. Can this be
> done in ggplot2?
> Thanks in advance for your help.
>
> Here is a small reproducable example:
>
> #This generates a small dataset
> h = 240
> R = seq(0.1,0.5,by=0.1)
> d = seq(0.0,2.0, by=0.1)
> Rd = rep(R, length(d))
> dd = rep(d, each = length(R))
> FSea = Rd * 2 * 0.03
> Er = FSea*dd / (1 + FSea*h*dd)
>
> dset = data.frame(cbind(Er,dd,Rd))
>
> #Some theme elements
> graph1 = theme(aspect.ratio = 1, panel.background = element_blank(),
> axis.line = element_line(), panel.grid.minor =
> element_blank(),panel.grid.major = element_blank(), plot.background =
> element_blank())
>
> p = ggplot(data = dset, aes(x = dd, y = (Er*3600)))
> p + geom_line(aes(linetype = factor(Rd), group = factor(Rd))) +
> scale_x_continuous("Density") + scale_y_continuous("Encounters") +
> guides(linetype = guide_legend("Reaction")) +
> theme(legend.position=c(0.85,0.35), panel.background = element_blank()) +
> graph1

Jannemann

unread,
Oct 13, 2012, 10:15:22 AM10/13/12
to ggp...@googlegroups.com
Hi Jo, 
while this deletes the background for me, the grey in the legend is still there:

p + geom_line(aes(linetype = factor(Rd), group = factor(Rd)))  + theme(panel.background = element_rect(fill = "transparent",colour = NA), plot.background = element_rect(fill = "transparent",colour = NA), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), plot.background = element_rect(fill = "transparent",colour = NA))

I am using version 0.9.2.1 on a mac.

Greetings, 

Jan

Johannes Björk

unread,
Oct 13, 2012, 11:11:22 AM10/13/12
to Jannemann, ggp...@googlegroups.com
Hi,

Have you tried experiment with the  legend.background = theme_rect(fill="white") parameter? 
Maybe it's even possible to get it to transparent. 

Cheers
Jo


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Oct 13, 2012, 11:45:58 AM10/13/12
to Johannes Björk, Jannemann, ggp...@googlegroups.com
I think fill=NA would give you "transparent". It usually does for other attributes. 

Jannemann

unread,
Oct 14, 2012, 11:24:02 AM10/14/12
to ggp...@googlegroups.com, Johannes Björk, Jannemann
Hi Jo and Brandon, 
thanks a lot for your help. After a reading more about updating theme options I found the element which had to be changed, and together with your hints I came to that solution:

+ theme(legend.key = element_rect(fill = NA, colour = NA, size = 0.25))

This removes the grey background and the black line around it. 

Greetings, 

Jan


Reply all
Reply to author
Forward
0 new messages