[英]Plotting Multiple Lines for Data Frame
I have a data frame that looks like this: 我有一个看起来像这样的数据框:
Date Something Values
2013-01-01 A 1093
2013-01-01 B 123
2013-01-01 C 4352
2013-01-02 C 13
2013-01-02 B 768
2013-01-02 A 56
2013-01-03 A 37
2013-01-03 C 1033
2013-01-03 B 3
2013-01-04 A 5
....
I would like to plot "Date" on the X axis and "Values" on Y axis for each of "Something". 我想为每个“东西”在X轴上绘制“日期”,在Y轴上绘制“值”。
How can I do that in R? 我如何在R中做到这一点?
Just write the following: 只需编写以下内容:
library(ggplot2)
a<-structure(list(Date = structure(c(15706, 15706, 15706, 15707,
15707, 15707, 15708, 15708, 15708, 15709), class = "Date"), Something = structure(c(1L,
2L, 3L, 3L, 2L, 1L, 1L, 3L, 2L, 1L), .Label = c("A", "B", "C"
), class = "factor"), Values = c(1093L, 123L, 4352L, 13L, 768L,
56L, 37L, 1033L, 3L, 5L)), .Names = c("Date", "Something", "Values"
), row.names = c(NA, -10L), class = "data.frame")
ggplot(a, aes(x=Date, y=Values, color=Something, group=Something)) + geom_line()
where a is your data.frame according to your post. 根据您的帖子,其中a是您的data.frame。 It will look something like:
它看起来像:
If your intent is to plot a separate line(or character) for each subset on a single chart area, there are a variety of tricks with plyr
you can use to organize your data in a useful way. 如果您打算在单个图表区域上为每个子集绘制一条单独的线(或字符),可以使用
plyr
的多种技巧来以有用的方式组织数据。 For simplicity, here's a long way to do it. 为了简单起见,这是一个很长的路要走。
Val_A<-dataframe[dataframe$Something=="A",]
Val_B<-dataframe[dataframe$Something=="B",]
Val_C<-dataframe[dataframe$Something=="C",]
(etc) (等等)
plot(Val_A$Date,Val_A$Values)
points(Val_B$Date,Val_B$Values,col='red')
points(Val_C$Date,Val_C$Values,col='green')
(etc) (等等)
As you can imagine, there are much more compact ways to do this. 您可以想象,有很多更紧凑的方法可以做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.