r语言 - 为 ggplot2 中的子集分配固定颜色



>我有两个数据表:

dt1 <- data.table(height=1.7+rnorm(100,0,0.3), year=1:100, specy=rep("B",100))
dt2 <- data.table(height=2+rnorm(100,0,0.5), year=1:100, specy=rep("A",100))

我可以单独绘制这组数据中的每一个。以下命令将在每个图形上给出相同的颜色:

ggplot()+geom_point(aes(year, height,colour=specy), dt1)
ggplot()+geom_point(aes(year, height,colour=specy), dt2)

我该如何修复,一个是紫色,另一个是绿色?

此外,当我绘制组合图时:

ggplot()+geom_point(aes(year, height,colour=specy), rbindlist(list(dt2,dt1)))

我希望每个物种的两张不同图表上都出现过相同的颜色......我目前不知道如何处理它。

谢谢!

 + scale_colour_manual(values = c("red","blue", "green"))

这应该会有所帮助,您还可以添加颜色名称,例如

 + scale_colour_manual(values = c(A="red",B="blue"))

另一种解决方案是:

ggplot()+geom_point(dt1, aes(year, height),colour="red")

最新更新