R中具有相同时间x轴的多条曲线 [英] Multiple curves with same Time x-axis in R
本文介绍了R中具有相同时间x轴的多条曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请您帮我解决这个问题。实际上,我想在带有R标记的x轴的R的同一图形上绘制多条曲线。
Could-you help me please in solving this issue. In fact, i would like to plot multiple curves on the same graph on R with a x-axis which is time labeled.
我尝试过此操作:
dayTime = strptime(sapply(c(0:110)+480, function(x){paste(floor(x/60),":",x%%60, sep="")}), "%H:%M")
n = 10
pdf("myGraph.pdf")
plot(x=dayTime, y=rep(0, length(dayTime)), main="myGraph", xlab="Time", ylab="Level", type="n", ylim=c(0, 0.05), xaxt = "n")
for(i in 1:n)
{
lines(myData[, i]), col=i)
}
r = as.POSIXct(round(range(dayTime), "hours"))
axis.POSIXct(1, at=seq(r[1], r[2], by="hour"), format="%H")
legend("topleft", legend=stockspool, col=c(1:n), lwd=rep(1, n), cex=0.8)
dev.off()
但问题是我不能在这种情况下添加带有线的曲线,但是如果我仅使用plot绘制一条曲线,它就可以正常工作。
but the problem is that i can not add a curve with lines in this context, but if i plot only one curve using plot, it works fine.
非常感谢。 / p>
Thank you very much.
推荐答案
以下解决方案使用 ggplot2
:
首先创建一些示例数据:
First create some sample data:
df = data.frame(id = rep(letters[1:5], each = 100),
time = rep(Sys.time() + 1:100, 5),
value = runif(500) + rep(1:5, each = 100))
> head(df)
id time value
1 a 2013-06-17 14:02:37 1.368671
2 a 2013-06-17 14:02:38 1.302188
3 a 2013-06-17 14:02:39 1.817873
4 a 2013-06-17 14:02:40 1.283439
5 a 2013-06-17 14:02:41 1.022949
6 a 2013-06-17 14:02:42 1.232590
并创建一个图。
library(ggplot2)
ggplot(df, aes(x = time, y = value, color = id)) + geom_line()
这篇关于R中具有相同时间x轴的多条曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文