我想绘制 xy 变量的点线图并突出显示两个分组。我知道一些区分因素的选项,例如fill、shape或。对于第一组,我想要有颜色,对于第二组形状(可能有也可能没有相同的颜色)。我需要一个图例来区分这两个分组(我已经有了)。也许我必须将 aes 放入 geom_line 或 geom_point 中,但我不确定。因为后来我想调整形状的大小(以更好地区分这些形状)。colgroup
这是我的代码:
library(ggplot2)
data <- data.frame(id1=c(1,1,1,2,2,2,3,3,3,4,4,4),
id2=seq(1:3), year=seq(from=2007, to=2018, by=1),
variable=rep(c(5:8), each=3))
# two groups by color and shape, but it drops the line (seperate legends, thats nice)
ggplot(data, aes(x=year, y=variable, col=factor(id1), shape=factor(id2))) +
geom_line() + geom_point()
Run Code Online (Sandbox Code Playgroud)
根据OP评论中的进一步信息,我们正在寻找这样的东西:
ggplot(data, aes(x=year, y=variable, col=factor(id1))) +
geom_line() +
geom_point(aes(shape=factor(id2), size = factor(id2))) +
labs(shape = "group 2", colour = "group 1", size = "group 2")
Run Code Online (Sandbox Code Playgroud)