gpt4 book ai didi

r - 使用 qplot 绘制线条

转载 作者:行者123 更新时间:2023-12-01 11:56:47 25 4
gpt4 key购买 nike

我想使用 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)

谢谢

最佳答案

使用 reshape 包melt m:

library(reshape)
library(ggplot2)

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

您可以根据自己的喜好重命名新 data.frame 中的列。这里的技巧是使用一个因子来表示您要绘制的每条线。

关于r - 使用 qplot 绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com