简体   繁体   English

不要在 2 图例图 (R ggplot2) 中的颜色图例中显示形状

[英]Don't show shape in color legend in a 2 legend plot (R ggplot2)

In my plot with 2 legends (one for shape and one for color), my boss finds it confusing that the color legend already has selected one of the shapes.在我有 2 个图例(一个用于形状,一个用于颜色)的情节中,我的老板发现颜色图例已经选择了其中一种形状令人困惑。 An example:一个例子:

a <- data.frame(name = c("A","B","C","D"),
                type = c("dog","dog", "cat", "cat"),
                location = c("house", "house", "house", "garden"),
                count = c(3,1,5,8))

ggplot(a, aes(x=name, y=count, color=location, shape=type)) +
  geom_point(size=7)

produces the following:产生以下内容:在此处输入图片说明

In this example, the 'location' legend is explaining the colors by showing colored circles (but circles also mean cat, which is apparently confusing).在此示例中,“位置”图例通过显示彩色圆圈来解释颜色(但圆圈也表示猫,这显然令人困惑)。 How can I force the 'location' legend to JUST show color, not a shape?如何强制“位置”图例仅显示颜色,而不是形状? Maybe showing location as red and blue rectangles would be ideal.也许将位置显示为红色和蓝色矩形会比较理想。

You can use function guides() and override.aes= to change the shape to rectangule for the color legend.您可以使用函数guides()override.aes=将颜色图例的形状更改为矩形。 Then you can remove background from legend keys for better look with theme() .然后您可以使用theme()从图例键中删除背景以获得更好的外观。

ggplot(a, aes(x=name, y=count, color=location, shape=type)) +
      geom_point(size=7)+
      guides(color=guide_legend(override.aes=list(shape=15)))+
      theme(legend.key=element_blank())

在此处输入图片说明

For removing the shape legend, you could do the following:要删除形状图例,您可以执行以下操作:

ggplot(a, aes(x=name, y=count, color=location, shape=type)) +
geom_point(size=7) + 
guides(shape=FALSE)

This removes the shape legend, but I'm not sure about changing the shape in the colour legend.这将删除形状图例,但我不确定更改颜色图例中的形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM