2012-12-21 53 views
3

我试图做一个正常绘制大多数数据点的绘图,但是具有不同大小符号的一组数据点。我希望图例显示相同:通常显示的大多数点,但用不同大小的符号绘制的异常。这里是一个代码短位:ggplot2:使图例中的符号与图中的符号匹配

library(ggplot2) 
x = c(1,2,1,2,3) 
y = c(1,2,3,4,3) 
vendor = c("x", "x", "y", "y", "z") 
df = data.frame(x,y,vendor) 

p <- ggplot(df) + 
    aes_string(x="x", y="y", color="vendor") + 
    geom_point(size=3, data=subset(df, vendor!="z")) + 
    geom_point(size=5, data=subset(df, vendor=="z")) 
ggsave("foo.pdf") 

的问题是,在所得到的图例,所有点绘制具有较大(size=5)符号,而不仅仅是那些与供应商ž。我希望供应商z用图例中的较大点绘制,其他图则用size=3绘制。 (奖金问题:我真正想要的是一个更大的粗略符号:我想要一个甜甜圈,而不是一个圆圈,我意识到shape=2会绘制一个轮廓圆,但它非常薄,我宁愿有。较厚的概述圆我想要做同样的用三角形的任何简单的方法来做到这一点)

也许我加错了,但以下这些建议:?

ggplot2: Making changes to symbols in the legend

与加的“指南”线没有帮助:

guides(size = guide_legend(override.aes = list(shape = 1))) 

即相同的输出,符号为图例中所有三个供应商的size=5

编辑:梦幻般的答案,我很快实施。现在,我已经添加了几行:

library(ggplot2) 
x = c(1,2,1,2,3) 
y = c(1,2,3,4,3) 
vendor = c("x", "x", "y", "y", "z") 
df = data.frame(x,y,vendor) 

df$vendor_z <- df$vendor=="z"  # create a new column 

ggplot(df) + 
    aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") + 
    geom_point() + 
    geom_line(size=1.5) + # this is the only difference 
    scale_size_manual(values = c(3, 5), guide = FALSE) 
    guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5)))) 

ggsave("foo.pdf") 

现在传说的大小是回落到3再次为所有的点,包括与供应商z中的人。有想法该怎么解决这个吗?

+0

BTW我是总R n00b所以随时批评我的代码,并帮助我它更好! – jowens

回答

4

尺寸不适用于图例,因为sizeaes_string之外。此外,如果您创建指示是否vendor == "z"的附加列,ggplot的工作将更容易。

下面是部分1的解决方案:

df$vendor_z <- df$vendor=="z"  # create a new column 

ggplot(df) + 
    aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") + 
    geom_point() + 
    scale_size_manual(values = c(3, 5), guide = FALSE) + 
    guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5)))) 

注意vendor_z是为aes_string参数。这将告诉ggplot创建size特性的图例。在功能scale_size_manual中,设置了size的值。此外,guide = FALSE仅为size避免了第二个图例。最后,size值应用于color图例。

enter image description here

第2部分:一个“环形”符号

为圆线的大小不能在ggplot进行修改。这里是一个解决方法:

ggplot(df) + 
    aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") + 
    geom_point() + 
    geom_point(data = df[df$vendor_z, ], aes(x = x, y = y), 
      size = 3, shape = 21, fill = "white", show_guide = FALSE) + 
    scale_size_manual(values = c(3, 5), guide = FALSE) + 
    guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5)))) 

这里,单个点使用绘制geom_point和数据的一个子集(df[df$vendor_z, ])。我选择了size3,因为这是小圈子的价值。 shape21是可以指定fill颜色的圆。最后,show_guide = FALSE避免了图例特征被新的shape覆盖。

enter image description here

编辑:第3部分:添加行

你可以打压geom_line传说的说法show_guide = FALSE

ggplot(df) + 
    aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") + 
    geom_point() + 
    geom_line(size=1.5, show_guide = FALSE) + # this is the only difference 
    scale_size_manual(values = c(3, 5), guide = FALSE) + 
    guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5)))) 

enter image description here

+0

谢谢Sven!多好的答案。我在问题中添加了一些问题,以及如何使用geom_line进行这项工作。 – jowens

+0

并且在甜甜圈上......任何让甜甜圈也出现在传奇中的方式?好的黑客,在蓝色的圆圈上绘制白色。 – jowens

+0

@jowens我用包含行的解决方案扩展了答案。在图例中的三个符号中的一个上绘制蓝色圆圈上的白色将是困难的,并且可能涉及ggplot之外的黑客。如果我能找到答案,我会让你知道的。 –