使用qplot绘制线条

mon*_*ing 3 r ggplot2

我想在ggplot2包中使用qplot在样本图上绘制多条线.但是我遇到了一些问题.

使用旧的情节和线条功能,我会做类似的事情

m<-cbind(1:4,5:8,-(5:8))
colnames(m)<-c("time","y1","y2")
m<-as.data.frame(m)
> m
  time y1 y2
1    1  5 -5
2    2  6 -6
3    3  7 -7
4    4  8 -8
plot(x=m$time,y=m$y1,type='l',ylim=range(m[,-1]))
lines(x=m$time,y=m$y2)
Run Code Online (Sandbox Code Playgroud)

谢谢

kmm*_*kmm 5

使用重塑包到meltm:

library(reshape)
library(ggplot2)

m2 <- melt(m, id = "time")
p <- ggplot(m2, aes(x = time, y = value, color = variable))
p + geom_line() + ylab("y")
Run Code Online (Sandbox Code Playgroud)

您可以根据自己的喜好重命名新data.frame中的列.这里的诀窍是有一个表示你想要绘制的每条线的因子.

  • `qplot(time,value,data = m2,color = variable,geom ="line")+ ylab("y")`是`qplot`语法的翻译. (2认同)