【问题标题】:Displaying dates on xaxis in readable format R以可读格式 R 在 xaxis 上显示日期
【发布时间】:2012-03-15 19:24:27
【问题描述】:

我有一个数据框,其中包含几个月的日期和 CPU 利用率数据。我可以像这样创建一个平滑的 gplot:

qplot(Date, CPU, data=app1, geom=c("line", "smooth"), method = "lm", 
  ylab="CPU", xlab="Date", main=")

这不显示日期,它只显示几个日期。是否可以显示重要的日期,例如日期大于或小于平滑线?

再次,如果我问了太多问题,我很抱歉。我只是在学习 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

【问题讨论】:

  • 在提问前请阅读how to make a reproducible example。此外,如果您在要求别人为您做之前表现出一些自己尝试过的迹象,您会得到更有利的回应。
  • if the date is greater than or smaller than smoothed line 是什么意思?
  • 我无法在 xaxis 中显示所有日期,空间不足。也许我想在 CPU 运行和平滑线爆裂时显示日期
  • 显示示例数据——即显示dput(app1)dput(head(app1)) 的输出。正如我们多次说过的那样:可重复的示例,否则问题就没有真正的意义。
  • 这听起来确实是一个有趣的问题,但是@mike smith,您需要提供比这更多的数据才能使您的问题可重现。您给出的三个观察结果甚至不足以构成您的情节。为什么不放足够的数据(你能留出 100 个观察值吗?),这样我们就可以用平滑线以及 CPU 变量和平滑线的至少一个交点来重现你的绘图?这会让别人更容易帮助你。

标签: r ggplot2


【解决方案1】:

你想要什么还不清楚,但我会尝试一下。

让我们从使您的数据集可重现开始。

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")

这里,Date 列属于 Date 类;我不知道这是否是您所拥有的(无法从您发布的内容中看出;这就是为什么要求提供完全可重现的示例)。

将您的 qplot 语法转换为 ggplot 语法(并添加点以便我可以更轻松地看到发生了什么):

ggplot(app1, aes(x=Date, y=CPU)) +
  geom_point() +
  geom_line() +
  geom_smooth(method="lm")

您的评论

这不显示日期,它只显示几个日期。是否可以显示重要的日期,例如日期大于或小于平滑线?

令人困惑。在 x 轴上,当然只显示了一些日期。您不希望每个点都被标记。每个点都在平滑线的一侧或另一侧。因此,我将您的请求解释为在图表上标记位于图表上绘制的置信区间之外的点。如果这不是您的意思,那么您需要提供更多详细信息。

为此,您无需让ggplot2 进行建模,而是自己进行。

mdl <- lm(CPU~Date, data=app1)
app2 <- cbind(app1, predict(mdl, interval="confidence"))

这样,原图就可以重现了。

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")

现在有了这个单独的数据集,您可以进一步注释哪些点是极端的并且应该被标记。

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)

您可以对文本进行更多格式化以使其更好看。这是一个例子。

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)

编辑

您可以在轴上拉出您想要的日期并将其传递给scale_x_date()breaks 参数。

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))

【讨论】:

  • 这是一个很好的例子,非常有趣!如果我们只是想让 x 轴标签只出现在 CI 区域之外的那些数据点上呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 2017-04-03
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多