R 组合geom_point()图例

R 组合geom_point()图例,r,ggplot2,R,Ggplot2,如何在下面的代码中将这两个图例合并为一个“物种”图例 library(ggplot2) data(iris) ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(color = "red", size = Species)) 谢谢。如果您想添加每个类的样本量信息,我更愿意以这种方式可视化数据 cat_table <- table(iris$Species) ggplot(iris, aes(x

如何在下面的代码中将这两个图例合并为一个“物种”图例

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

谢谢。

如果您想添加每个类的样本量信息,我更愿意以这种方式可视化数据

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)))

cat_table如果您想添加每个类的样本量信息,我更愿意以这种方式可视化数据

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)))
cat_table只要去掉美学中的“颜色”。这似乎就是你想要的

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

如果你想保持色彩的美感,那么 您可以在“尺寸图例”指南中手动替代颜色

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")))) 

只要去掉美学中的“颜色”。这似乎就是你想要的

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

如果你想保持色彩的美感,那么 您可以在“尺寸图例”指南中手动替代颜色

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")))) 

ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species))@Puriney谢谢,但我需要使用单一颜色并通过大小/形状属性显示差异。我想知道您是否想将
color=“red”
移到
aes()之外
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(size=Species),color=“red”)
@jazzurro之前没有看到您的评论。正如我对下面答案的评论,这正是我想要的——谢谢@马诺吉特:不用担心ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species))@Puriney谢谢,但我需要使用一种颜色并通过大小/形状属性显示差异。我想知道您是否想将
color=“red”
移到
aes()之外
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(size=Species),color=“red”)
@jazzurro之前没有看到您的评论。正如我对下面答案的评论,这正是我想要的——谢谢@马诺吉特:不用担心这个方法对于这个简单的例子非常有效,但是我的实际数据集有大量的类别,添加太多的颜色会使它变得杂乱无章。这就是为什么我希望使用一种颜色和不同的形状/大小。这个方法对于这个简单的示例非常有效,但是我的实际数据集有大量的类别,添加太多的颜色会使它变得混乱。这就是为什么我希望使用一种颜色和不同的形状/大小。保持
颜色
远离
aes()
正是我想要的-谢谢!保持
color
远离
aes()
正是我想要的-谢谢!