从另一个data.frame向qplot添加一行 [英] Add a line from another data.frame to qplot
问题描述
是否可以在已有的绘图中添加一条线(例如附加的时间序列)? 我知道如何添加水平或垂直线,但是如何从其他data.frames中添加?
Is it possible to add a line (such as an additional time series) to an already existing plot? I know how to add horizontal or vertical lines, but how can I add from other data.frames?
q_myplot<-qplot(datefield,myvalue,data=mydf,geom=c("line"),colour=category) +opts(axis.title.x = theme_blank()) + scale_x_date(major="2 years")
是我的基本情节,显示了按类别分组的三个不同的时间序列.有没有一种方法,例如通过使用图层将另一条线添加到绘图中?当然,我可以使用其他类别将这些附加数据添加到mydf中,但是我想知道是否还有更好的主意.
is my basic plot, showing three different time series grouped by category. Is there a way to add another line to the plot for example by using layers? Of course I could add this additional data to mydf using another category, but I wonder if there's a better idea out there.
推荐答案
通过使用ggplot()而不是qplot(),您可以拥有更大的灵活性. 这是一个使用两个数据集的最小示例:
by using ggplot() instead of qplot(), you can have more flexibility. here is a minimal example using two datasets:
d1 <- data.frame(x1=rep(1:10,3), y1=rnorm(10*3), g1=gl(3,10,labels=letters[1:3]))
d2 <- data.frame(x2=rep(1:10,3), y2=rnorm(10*3), g2=gl(3,10, labels=letters[4:6]))
ggplot() +
geom_line(aes(x1, y1, colour=g1), d1) +
geom_line(aes(x2, y2, colour=g2), d2)
这篇关于从另一个data.frame向qplot添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!