我有这样的数据集
data <- data.frame(Time=as.Date(c("2007-01-31", "2007-02-28", "2007-03-31", "2007-04-30",
"2007-05-31", "2007-06-30", "2007-07-31", "2007-08-31", "2007-09-30", "2007-10-31",
"2007-11-30", "2007-12-31"), format="%Y-%m-%d"),
a=c(NA, NA, 1.201, NA, NA, 1.206, NA, NA, 1.212, NA, NA, 1.237),
b=c(1.187, 1.201, 1.206, 1.219, 1.211, 1.187, 1.202, 1.213, 1.209, 1.208,
1.219, 1.237),
c=c(1.198, 1.201, 1.203, 1.206, 1.207,1.207, 1.208, 1.21, 1.214, 1.22, 1.228, 1.236))
> data
Time a b c
1 2007-01-31 NA 1.187 1.198
2 2007-02-28 NA 1.201 1.201
3 2007-03-31 1.201 1.206 1.203
4 2007-04-30 NA 1.219 1.206
5 2007-05-31 NA 1.211 1.207
6 2007-06-30 1.206 1.187 1.207
7 2007-07-31 NA 1.202 1.208
8 2007-08-31 NA 1.213 1.210
9 2007-09-30 1.207 1.209 1.214
10 2007-10-31 NA 1.208 1.220
11 2007-11-30 NA 1.219 1.228
12 2007-12-31 1.219 1.237 1.236
我想要一个带有点的ggplot和一个用于b&amp;的行。 C。我想出了
ggplot(data, aes(x=Time)) + geom_point(aes(y=a, colour="a"), size=3) +
geom_line(aes(y=b, colour="b")) + geom_line(aes(y=c, colour="c"))
问题是,我有更多变量,比如b&amp; c,我想绘制。有没有更有效的方法呢?
答案 0 :(得分:0)
这样做的一种方法是使用tidyr包(和一些dplyr)
library('dplyr')
library('tidyr')
data2 <- gather(data, grp, value, a:c)
ggplot(data2, aes(x = Time, y = value, color = grp, group = grp)) +
geom_point(data = filter(data2, grp == 'a'), size = 3) +
geom_line()
情节