开发者

Getting legend coloring in an empty ggplot

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_co开发者_运维知识库ntinuous(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.


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


A simple way to do this would be to use the size option:

ggplot(vas, aes(x=time, y=pain, colour=light.color, size=light.intensity)) 
 + geom_point(size=0)


Could you plot two geom_points() - one with the color of your background?

ggplot(cars, aes(x=speed, y=dist))+
geom_point(aes(col=speed))+
geom_point(colour="white")+
theme_bw()


In the end, taking Aniko's suggestion along (that I initially couldn't get to work, so discarded, unrightly so) I came up with the following code.

vas2 <- vas
vas2$time <- vas2$time+181
pp <- p %+% vas2 #p is the above plot assigned to a variable.
pp + scale_x_continuous(name="Time (minutes)", limits=c(0,180)) 

So it was about just shifting around your data on an axis and then excluding that part of

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜