繁体   English   中英

ggplot图例与geom_rect

[英]ggplot legend with geom_rect

我正在尝试为geom_rect图例创建图例项。 但是,图例也显示了形状,而不是简单的灰色矩形(请参见下面的示例)。

有没有一种创建图例项的方法,该图例项仅显示geom_rect的灰色矩形和数据点的填充形状?

library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width))+ 
  geom_rect(mapping=aes(xmin=5.5,
                         xmax=6.5,
                          ymin=2.5,
                         ymax=3.5,fill="historic range"))+
  geom_point(aes(Sepal.Length, Sepal.Width,fill="Data points",shape="Data points")) +
  scale_fill_manual(values=c("darkgreen","grey"),labels=c("Data points","historic range")) +
  scale_shape_manual(values=c(22),labels=c("Data points"))

上面代码的结果: 在此处输入图片说明

尝试这个

library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_rect(aes(xmin=5.5, xmax=6.5, ymin=2.5, ymax=3.5, fill="Historic range")) +
  geom_point(aes(x=Sepal.Length, y=Sepal.Width, shape="Data points"), fill='red') +
  scale_fill_manual(values="grey", labels=c("Historic range")) +
  scale_shape_manual(values=c(22), labels=c("Data points"))

在此处输入图片说明

暂无
暂无

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

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