我有一个数据框,有几个月的日期和CPU利用率数据.我可以像这样创建一个平滑的gplot:
qplot(Date, CPU, data=app1, geom=c("line", "smooth"), method = "lm",
ylab="CPU", xlab="Date", main=")
Run Code Online (Sandbox Code Playgroud)
这不显示日期,只显示几个日期.如果日期大于或小于平滑线,是否可以显示重要日期?
再次,如果我问了太多问题,我很抱歉.我只是在学习R并经历了第一次痛苦.
数据看起来像这样:
Date CPU
3/10/2012 0:00 28.7
3/9/2012 0:00 94.1
3/2/2012 0:00 82.7
2/23/2012 0:00 68.5
2/22/2012 0:00 67.4
2/10/2012 0:00 100
2/6/2012 0:00 100
2/4/2012 0:00 89.4974
2/3/2012 0:00 100
2/1/2012 0:00 100
1/29/2012 0:00 57.4693
1/25/2012 0:00 100
1/21/2012 0:00 98.2085
1/20/2012 0:00 99.9987
1/19/2012 0:00 99.9698
1/17/2012 0:00 99.9802
1/15/2012 0:00 51.5781
1/14/2012 0:00 86.5854
1/12/2012 0:00 100
1/10/2012 0:00 100
1/8/2012 0:00 48.3474
1/6/2012 0:00 99.9833
1/5/2012 0:00 100
1/2/2012 0:00 100
12/31/2011 0:00 99.6901
12/25/2011 0:00 76.543
12/21/2011 0:00 99.9536
12/19/2011 0:00 100
12/16/2011 0:00 99.9807
12/14/2011 0:00 99.9995
12/6/2011 0:00 100
3/8/2012 0:00 83.2
3/7/2012 0:00 67.7
3/6/2012 0:00 70.8
3/5/2012 0:00 92.6
2/27/2012 0:00 77.3
2/24/2012 0:00 74.1
2/21/2012 0:00 79.3
2/19/2012 0:00 57.8052
2/18/2012 0:00 99.9938
2/14/2012 0:00 100
2/9/2012 0:00 100
2/8/2012 0:00 100
2/7/2012 0:00 100
2/5/2012 0:00 57.478
2/2/2012 0:00 100
1/31/2012 0:00 100
1/30/2012 0:00 100
1/28/2012 0:00 87.604
1/27/2012 0:00 100
1/24/2012 0:00 100
1/23/2012 0:00 100
1/18/2012 0:00 100
1/16/2012 0:00 99.9477
1/13/2012 0:00 99.9979
1/9/2012 0:00 100
1/7/2012 0:00 92.6704
1/4/2012 0:00 100
1/3/2012 0:00 100
1/1/2012 0:00 17.501
12/28/2011 0:00 100
12/27/2011 0:00 100
12/23/2011 0:00 99.999
12/22/2011 0:00 100
12/20/2011 0:00 99.9865
12/18/2011 0:00 8.2211
12/15/2011 0:00 100
Run Code Online (Sandbox Code Playgroud)
你想要的仍然不清楚,但我会捅它.
让我们从使数据集重现开始.
app1 <-
structure(list(Date = structure(c(15409, 15408, 15401, 15393,
15392, 15380, 15376, 15374, 15373, 15371, 15368, 15364, 15360,
15359, 15358, 15356, 15354, 15353, 15351, 15349, 15347, 15345,
15344, 15341, 15339, 15333, 15329, 15327, 15324, 15322, 15314,
15407, 15406, 15405, 15404, 15397, 15394, 15391, 15389, 15388,
15384, 15379, 15378, 15377, 15375, 15372, 15370, 15369, 15367,
15366, 15363, 15362, 15357, 15355, 15352, 15348, 15346, 15343,
15342, 15340, 15336, 15335, 15331, 15330, 15328, 15326, 15323
), class = "Date"), CPU = c(28.7, 94.1, 82.7, 68.5, 67.4, 100,
100, 89.4974, 100, 100, 57.4693, 100, 98.2085, 99.9987, 99.9698,
99.9802, 51.5781, 86.5854, 100, 100, 48.3474, 99.9833, 100, 100,
99.6901, 76.543, 99.9536, 100, 99.9807, 99.9995, 100, 83.2, 67.7,
70.8, 92.6, 77.3, 74.1, 79.3, 57.8052, 99.9938, 100, 100, 100,
100, 57.478, 100, 100, 100, 87.604, 100, 100, 100, 100, 99.9477,
99.9979, 100, 92.6704, 100, 100, 17.501, 100, 100, 99.999, 100,
99.9865, 8.2211, 100)), .Names = c("Date", "CPU"), row.names = c(NA,
-67L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
这里,Date列是Date类; 我不知道这是不是你有没有(不能告诉你发布的内容;这就是为什么要求一个完全可重复的例子).
将qplot语法转换为ggplot语法(并添加点,以便我可以更容易地看到发生的事情):
ggplot(app1, aes(x=Date, y=CPU)) +
geom_point() +
geom_line() +
geom_smooth(method="lm")
Run Code Online (Sandbox Code Playgroud)

你的评论
这不显示日期,只显示几个日期.如果日期大于或小于平滑线,是否可以显示重要日期?
令人困惑.在x轴上,当然只显示一些日期.你不希望标记每一点.并且每个点都在平滑线的一侧或另一侧.因此,我将您的请求解释为标记图表上超出图表上绘制的置信区间的点.如果这不是您的意思,那么您需要提供更多细节.
为了做到这一点,你需要不ggplot2进行建模,而是自己做.
mdl <- lm(CPU~Date, data=app1)
app2 <- cbind(app1, predict(mdl, interval="confidence"))
Run Code Online (Sandbox Code Playgroud)
这样,可以再现原始图形.
ggplot(app2, aes(x=Date)) +
geom_point(aes(y=CPU)) +
geom_line(aes(y=CPU)) +
geom_smooth(aes(y=fit, ymin=lwr, ymax=upr), stat="identity")
Run Code Online (Sandbox Code Playgroud)

现在使用这个单独的数据集,您可以进一步注释哪些点是极端的并且应该被标记.
app2 <- transform(app2,
extreme = (CPU < lwr) | (CPU > upr))
ggplot(app2, aes(x=Date)) +
geom_point(aes(y=CPU)) +
geom_line(aes(y=CPU)) +
geom_smooth(aes(y=fit, ymin=lwr, ymax=upr), stat="identity") +
geom_text(aes(label=as.character(Date), y=CPU), data=app2[app2$extreme,],
size=3, angle=90)
Run Code Online (Sandbox Code Playgroud)

您可以对文本进行更多格式化以使其更好.这是一个例子.
app2 <- transform(app2,
hadj = ifelse(extreme, ifelse(CPU < lwr, 1.1, -0.1), NA))
ggplot(app2, aes(x=Date)) +
geom_point(aes(y=CPU)) +
geom_line(aes(y=CPU)) +
geom_smooth(aes(y=fit, ymin=lwr, ymax=upr), stat="identity") +
geom_text(aes(label=format(Date, "%b %d"), y=CPU, hjust=hadj),
data=app2[app2$extreme,],
size=3, angle=90)
Run Code Online (Sandbox Code Playgroud)

编辑
你可以在轴上拉出你想要的日期并将其传递给breaks参数scale_x_date().
extremedates = app2[app2$extreme,"Date"]
ggplot(app2, aes(x=Date)) +
geom_point(aes(y=CPU)) +
geom_line(aes(y=CPU)) +
geom_smooth(aes(y=fit, ymin=lwr, ymax=upr), stat="identity") +
scale_x_date(breaks=extremedates) +
opts(axis.text.x = theme_text(angle=90, size=5))
Run Code Online (Sandbox Code Playgroud)
