我正在尝试从具有几列的数据框在 R 中绘制一个图,我想让 ggplot 将其中一列绘制为点,将其他几列绘制为不同颜色的线。
我可以找到有关如何分别制作每个图的示例,但我似乎找不到合并图的命令...
感谢您的任何帮助,您可以提供。
像这样:
dat <- data.frame(points.x = c(1:10), points.y = c(1:10),
lines.x = c(10:1), lines.y = c(1:10))
ggplot(dat, aes(points.x, points.y)) + geom_point() +
geom_line(aes(lines.x,lines.y))
为了将几个不同的列绘制为不同颜色的线,请使用包中的melt
函数reshape2
。
例如:
df <- data.frame(A=1:10, B=rnorm(10), C=rnorm(10), D=rnorm(10))
melted <- melt(df, id="A")
ggplot(melted[melted$variable!="B",], aes(A, value, color=variable)) + geom_line() +
geom_point(data=melted[melted$variable=="B",])