我有一个数据框,数据框的内容如下所示:
V1 V2 V3 V4
1 2013-08-01 1769.00 1769.00 1769
2 2013-09-01 1106.00 1106.00 1106
3 2013-10-01 1180.00 1180.00 1180
4 2013-11-01 1401.00 1401.00 1401
5 2013-12-01 1327.00 1327.00 1327
6 2014-01-01 1673.00 1673.00 1673
7 2014-02-01 1455.00 1455.00 1455
8 2014-03-01 1819.00 1819.00 1819
9 2014-04-01 1964.00 1964.00 1964
10 2014-05-01 2474.00 2474.00 2474
11 2014-06-01 2255.00 2255.00 2255
12 2014-07-01 2183.00 2183.00 2183
13 2014-08-01 2016.00 2016.00 2016
14 2014-09-01 2271.00 2271.00 2271
15 2014-10-01 238.80 118.80 0
16 2014-11-01 149.25 74.25 0
17 2014-12-01 159.20 79.20 0
18 2015-01-01 189.05 94.05 0
19 2015-02-01 179.10 89.10 0
20 2015-03-01 228.85 113.85 0
21 2015-04-01 199.00 99.00 0
22 2015-05-01 248.75 123.75 0
23 2015-06-01 268.65 133.65 0
24 2015-07-01 338.30 168.30 0
25 2015-08-01 308.45 153.45 0
26 2015-09-01 298.50 148.50 0
我正在尝试将变量V2
,V3
和V4
中包含的数据与V1
中包含的时间进行对比。我希望第14行之后的条目具有不同的颜色。我编写了以下代码:
p <- ggplot() + geom_line(data=data[1:14,],aes(x=V1, y=V2), color='blue', size=1.5)
p <- p + geom_line(data=data[15:26,],aes(x=V1, y=V2), color='red', size=1.5)
p <- p + geom_line(data=data[15:26,],aes(x=V1, y=V3), color='green', size=1.5)
p <- p + geom_line(data=data[15:26,],aes(x=V1, y=V4), color='yellow', size=3) +
xlab('Time') + ylab('Revenue')
p
这给了我以下情节:
我想在情节中添加一个外部传奇,但我无法做到这一点。 任何指针都会非常感激。
答案 0 :(得分:1)
由于您没有为行1:14绘制V1 vs V3和V4,您可以使用geom_text添加额外信息:
data = structure(list(V1 = structure(c(15713, 15714, 15715, 15716, 15717,
16071, 16072, 16073, 16074, 16075, 16076, 16077, 16078, 16079,
16080, 16081, 16082, 16436, 16437, 16438, 16439, 16440, 16441,
16442, 16443, 16444), class = "Date"), V2 = c(1769, 1106, 1180,
1401, 1327, 1673, 1455, 1819, 1964, 2474, 2255, 2183, 2016, 2271,
238.8, 149.25, 159.2, 189.05, 179.1, 228.85, 199, 248.75, 268.65,
338.3, 308.45, 298.5), V3 = c(1769, 1106, 1180, 1401, 1327, 1673,
1455, 1819, 1964, 2474, 2255, 2183, 2016, 2271, 118.8, 74.25,
79.2, 94.05, 89.1, 113.85, 99, 123.75, 133.65, 168.3, 153.45,
148.5), V4 = c(1769L, 1106L, 1180L, 1401L, 1327L, 1673L, 1455L,
1819L, 1964L, 2474L, 2255L, 2183L, 2016L, 2271L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("V1", "V2",
"V3", "V4"), row.names = c(NA, -26L), class = "data.frame")
p <- ggplot() + geom_line(data=data[1:14,],aes(x=V1, y=V2), color='blue', size=1.5)
p <- p + geom_line(data=data[15:26,],aes(x=V1, y=V2), color='red', size=1.5)
p <- p + geom_line(data=data[15:26,],aes(x=V1, y=V3), color='green', size=1.5)
p <- p + geom_line(data=data[15:26,],aes(x=V1, y=V4), color='yellow', size=3) +
xlab('Time') + ylab('Revenue')
p <- p+ geom_text(aes(x= as.Date("2014-06-01", format="%Y-%d-%m"),y=50,label="Your info"))
p