R ggplot2:同时添加尺寸图例和颜色图例:仅自动显示一个

R ggplot2:同时添加尺寸图例和颜色图例:仅自动显示一个,r,ggplot2,aesthetics,R,Ggplot2,Aesthetics,数据集(原始过滤数据3): 或此处的dput输出: 我正试图画出下面的情节 为此,我采取了以下措施: ggplot(originalRfiltered3, aes(x = host.length, y = of.thorax) ) + geom_point(aes(size = originalRfiltered3$number.simp), col = "black") + geom_point(aes(col = host.sex), size = origi

数据集(原始过滤数据3):

或此处的dput输出:

我正试图画出下面的情节

为此,我采取了以下措施:

ggplot(originalRfiltered3, aes(x = host.length, y = of.thorax) ) + 
 geom_point(aes(size = originalRfiltered3$number.simp), col = "black") + 
 geom_point(aes(col = host.sex), size = originalRfiltered3$number.simp) + 
 stat_smooth(method = "lm", col = "black", formula = y~poly(x,2)) + 
 scale_colour_manual(name = "Host Sex", 
   values = c("red","blue"), labels=c("F","M")) + 
 scale_size_continuous(name = "Group Size",
   labels = c("1", "2", "3", "4+") ) + 
 theme_classic() + 
 labs(x = "Host length (cm)" ) +
 labs(y = "Parasite length (mm)" ) +
 labs(title = "Correlation between size of host and parasite")
我知道我可以在同一行中指定大小和颜色,但这只生成一个图例 让我们试试这个:

ggplot(originalRfiltered3, aes(x = host.length, y = of.thorax) ) + 
 geom_point(aes(size = number.simp, fill = host.sex), shape=21, colour="black" ) + 
 scale_fill_manual(name = "Host Sex", labels=c("F", "M"), values = c("red", "blue") ) + 
     scale_size_continuous(name = "Group Size", labels = c("1", "2", "3", "4+") ) + 
  stat_smooth(method = "lm", col = "black", formula = y~poly(x,2)) + 
  theme_classic() + 
  labs(x = "Host length (cm)" ) +
  labs(y = "Parasite length (mm)" ) +
  labs(title = "Correlation between size of host and parasite")  +
  guides(fill = guide_legend(override.aes = list(size=8)))

黑色圆圈是在
shape=21
中定义的,颜色是在
scale\u fill\u manual
中定义的。答案不是:我认为你应该在一个命令中编写所有内容。它提高了可读性。我认为保存
a
是没有必要的。谢谢(也谢谢你的编辑?)!我是ggplot2的新手,所以我仍然在为不同的结构绞尽脑汁:)@l是的,我已经尽了我的最大努力@L好了,再次感谢。我忽略了你问题顶部的dput链接。很抱歉你可以把它从问题中删除…太美了!多谢各位。最后一个问题:是否可以增加图例中M和F旁边的点的大小?它吹毛求疵,但我也找不到它的代码@Lstat因为它没有被调用,所以值得向OP@Lstat提及:这个答案很好地改变了
data$column
aes()
中不使用,只使用裸列名。在简单的绘图上,
data$column
可以工作,但如果使用facets或进行聚合的
stat.*
函数,则会中断。