colour of one geom 'leaking' into legend of another

37 views
Skip to first unread message

Olivia

unread,
Jul 5, 2013, 7:47:16 AM7/5/13
to ggp...@googlegroups.com
Hi there,

I have searched the group and forums, but apologies if this has been addressed previously.  My problem is that when I have a legend for geom_point size, colours elsewhere in the plot leak into the legend. However this is only visible intermittently (generally not when the plot is first drawn, but occurs both in R studio and R, and inescapably when I write the plot to a pdf or jpeg).  Please let me know if further information is required. An example of the problem is attached ("as in pdf version"), while "as in r studio" shows how it is first rendered.  The colour of the leak depends on the colour specified elsewhere in the plot.

#Some made up data:

repFrame <- data.frame(A= 1:20, B= rnorm(1:20), grades= rnorm(1:20),groupNum = rep(c("First group", "Second group"),each=10), class = rep(c("Class A", "Class B","Class C","Class D","Class E"),each=4))

testPlot <- ggplot(repFrame, aes(x=A, y = B))+
  geom_point(alpha=0.7, aes(size=groupNum,colour=grades))+
  geom_smooth(colour="orange")+
  geom_density2d(colour="pink",alpha=0.3)+
  theme_bw()
testPlot #ends up with pink lines across the back of the legend for size

testPlot2 <- ggplot(repFrame, aes(x=A, y = B))+
  geom_point(alpha=0.7, aes(size=groupNum,colour=grades))+
  geom_smooth(colour="orange")+
  geom_density2d(colour="forestgreen",alpha=0.3)+
  theme_bw()
testPlot2  #ends up with forestgreen lines across the legend for size.

# I thought the addition of the code below might fix the problem, but no.
theme_bw()+
  theme(legend.key=element_rect(fill="white"))+
  theme(legend.background=element_rect(fill="white"))

Kind regards,

Olivia
MSc candidate

as in rstudio.PNG
pdf version.pdf

baptiste auguie

unread,
Jul 5, 2013, 9:54:52 AM7/5/13
to Olivia, ggp...@googlegroups.com
Hi,

You can tell ggplot2 not to include a given layer in the guides,

geom_density2d(colour="pink", show_guide=FALSE)

which should fix your problem (but not the inconsistency of Rstudio's device, unfortunately...)

HTH,

b.



--
--
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
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Olivia

unread,
Jul 6, 2013, 2:46:30 AM7/6/13
to ggp...@googlegroups.com, Olivia
Excellent, thanks! I knew you could not show layers but since it was intermittent it hadn't occurred to me to try that as a solution. I have and it works on my real data, the graph looks much tidier now.

Kind regards,

Olivia
Reply all
Reply to author
Forward
0 new messages