结合geom_point()传说

时间:2018-02-03 22:23:11

标签: r ggplot2

如何将两个传说合并为一个" Species"下面的代码中的图例?

library(ggplot2)
data(iris)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = "red", size = Species))

enter image description here 感谢。

2 个答案:

答案 0 :(得分:2)

如果您想要添加每个类的样本大小信息,我希望以这种方式可视化数据。

cat_table <- table(iris$Species)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(color =Species)) + 
scale_color_manual(breaks=names(cat_table), labels=paste(names(cat_table), ':', cat_table), values=rainbow(n=length(cat_table)))

enter image description here

答案 1 :(得分:1)

只需删除&#34;颜色&#34;走出美学。这似乎是你所追求的。

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(size = Species), colour = "red") 

enter image description here

如果你想把颜色保持为审美,那么 您可以手动覆盖尺寸图例指南中的颜色

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(size = Species, colour = "red")) +
  guides(colour = FALSE, 
         size=(guide_legend(override.aes = 
                              list(colour = "red")))) 

enter image description here

相关问题