Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting legend coloring in an empty ggplot

Tags:

r

ggplot2

Before presenting the actual data, I would like to make a plot identical to the one with data but then without the data points in there. This helps me in explaining how to interpret such a plot without distracting the audience with the actual data that will be in the plot.

So in the code below I would basically want to exchange the geom_point() with geom_blank(). No problem.

However, this also removes the color and size information from the legends that the plot code creates. Is there a way to get this back?

ggplot(vas, aes(x=time, y=pain, colour=light.color, size=light.intensity)) + 
  #geom_point(na.rm=FALSE) +
  geom_blank() + 
  facet_wrap(~ppno) +
  scale_colour_manual(values=cols) +
  scale_y_continuous(name="VAS Pain (a.u.)") +
  scale_x_continuous(name="Time (minutes)")

What is the proper way to get the color indications back into the legend(s)? Now they only display the value(s) of the various levels of a certain parameter (color or size) but not the actual graphical element (a color or a size of a dot) that goes with a certain level.

like image 794
Paul Avatar asked Jun 28 '10 09:06

Paul


1 Answers

How about hiding the actual points outside the plotting window? Something along these lines:

ggplot(cars, aes(x=speed+100, y=dist))+ #move x values to the right
  geom_point(aes(col=speed))+
  scale_x_continuous(at=seq(5,125,by=5), limits=c(0,30)) #set plotting window
like image 117
Aniko Avatar answered Oct 05 '22 07:10

Aniko