在空ggplot中获取图例着色 [英] Getting legend coloring in an empty ggplot

查看:181
本文介绍了在空ggplot中获取图例着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在呈现实际数据之前,我想绘制一张与数据一致的图,但没有数据点。这有助于我解释如何解释这样的情节,而不会分散观众的真实数据。



所以在下面的代码中,我基本上想要用geom_blank()交换geom_point()。没问题。

但是,这也会从图表代码创建的图例中移除颜色和大小信息。有没有办法让它回来?

  ggplot(vas,aes(x = time,y = pain,color = 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(au))+
scale_x_continuous(name =Time(minutes))

什么是将颜色指示重新带回图例的正确方法。现在,它们只显示特定参数(颜色或大小)的各个级别的值,但不显示具有特定级别的实际图形元素(点的颜色或大小)。

解决方案

如何隐藏绘图窗口外的实际点?沿着这些线:

  ggplot(cars,aes(x = speed + 100,y = dist))+ #move x右边的值
geom_point(aes(col = speed))+
scale_x_continuous(at = seq(5,125,by = 5),limits = c(0,30))#set plotting window


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 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 (colour 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

这篇关于在空ggplot中获取图例着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆