【问题标题】:ggplot2 legend with only one category / with only the shape and no scaleggplot2 图例只有一个类别/只有形状,没有比例
【发布时间】:2016-03-27 13:36:43
【问题描述】:

我怎样才能有一个只有一个类别的图例部分?我试图弄乱override.aes,但没有成功。或者,可以将所需的输出视为只有形状但没有比例的图例。

ggplot(iris) +
  geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
  scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10))+
  guides(size = guide_legend( override.aes=list(range=  c(1,5)))) 

product 类型的插图我正在尝试访问:

点已按比例缩放,但图例未报告比例。

【问题讨论】:

  • 我没有完整的答案,但这可能会帮助您入门。我绘制了一个额外的 geom_point() (从 iris 的第一行) ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species), size=2)+ scale_size_continuous("Legend with \n 仅 1 个圆 ", label="") + geom_point(data=test, aes(x=Sepal.Width, y=Sepal.Length, size=Sepal.Length),color='red') + guides(size = guide_legend(override.aes = list(size = 3)))

标签: r ggplot2 maps


【解决方案1】:

只需在比例中创建一个break。您也可以为其添加自定义标签(此处为"")。您还可以通过选择的中断来控制图例中点的大小。

scale_color_discrete() 行在那里是因为否则 1 点图例会在顶部,而不是您想要的图片中的内容。

require(ggplot2)
g <- ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length,
                                   color = Species, size = Sepal.Length)) +
  scale_color_discrete(name = "Color") +
  scale_size_continuous(name = "Legend with \n only 1 circle",
                        breaks = 5, labels = "")

【讨论】:

    【解决方案2】:

    虽然@choff 解决方案是我给出的示例的最佳解决方案,但这是我最终使用的略有不同的解决方案,因为我需要控制圆圈的范围大小。

    ggplot(iris) +
      geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
      scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10), labels=c("","","","",""))+
      guides(size =guide_legend( override.aes=list(size=c(4,0,0)))) +
      theme(legend.key = element_blank())
    

    【讨论】:

      【解决方案3】:

      从您的目标地图图表中,您似乎希望您的图例按颜色和形状分隔图表符号。 Size 仅用于设置符号的大小。此外,您的数据可能会有一个列,分别列出有投资的国家和没有投资的国家。因此,我们可以向 iris 添加一列,将行通过两个值分隔,将颜色和形状映射到该列,然后在单个组合图例中显示这两种美学的图例。代码如下:

      sp <- ggplot(transform(iris, Flower_size = ifelse(Petal.Width < 1, "Small Flower","Big Flower"))) 
      sp <- sp + geom_point(aes(x=Sepal.Width, y=Sepal.Length, fill=Flower_size, shape=Flower_size, size=Sepal.Length), colour=NA)
      sp <- sp + scale_size_continuous(range = c(4,7))
      sp <- sp + scale_shape_manual(values=c(21, 22))
      sp <- sp + scale_fill_manual(values=c("grey", "orange"))
      sp <- sp + labs(fill="", shape="")
      sp <- sp + guides(size=FALSE, fill=guide_legend(override.aes=list(size=7)))
      sp <- sp + theme(legend.text=element_text(size=12)) 
       plot(sp)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 2019-07-28
        • 2021-05-29
        • 1970-01-01
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        相关资源
        最近更新 更多