以可读格式显示xaxis上的日期R [英] Displaying dates on xaxis in readable format R

查看:100
本文介绍了以可读格式显示xaxis上的日期R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,有几个月的日期和CPU利用率数据。我可以创建一个平滑的gplot,如下所示:

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

这并不显示日期,它只显示几个日期。是否可以显示日期,如日期大于或小于平滑线的重要日期?如果我问了太多问题,我很抱歉,我刚刚学习R并经历了第一次痛苦。



数据如下所示:

 日期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
2012年1月25日0:00 100
1/21/2012 0:00 98.2085
2012年1月20日0:00 99.9987
2012年1月19日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
2012年1月12日0 :00 100
1/12/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
2011/12/28 0:00 100
12/27/2011 0:00 100
2011/12/23 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

你想要的东西仍然不清楚,但是我会采取一些措施。 p>

让我们开始将您的数据集重现。

  app1 < -  
结构(列表(日期=结构(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,153 783,15377,15375,15372,15370,15369,15367,
15366,15363,15362,15357,15355,15352,15348,15346,15343,
15342,15340,15336,15335,15331, (28.7,94.1,82.7,68.5,67.4,100,
100,89.4974,100,100,57.4693,b
,class =Date),CPU = 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.28, 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, 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 语法(并且添加点以便我可以更容易地看到发生了什么) p>

  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 =身份)+
geom_text(aes(标签= as.character(日期),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 (日期,%b%d),y = CPU,hjust = hadj),
data = app2 [app2 $ extreme,],
size = 3,angle = 90)



编辑



您可以在轴上取出想要的日期并传递对于 scale_x_date()



<$ breaks (app2,aes(x = Date))+
geom_point(aes(aes(p)){code> extremedates = app2 [app2 $ extreme,Date]

ggplot y = CPU))+
geom_line(aes(y = CPU))+
geom_smooth(aes(y = fit,ymin = lwr,ymax = upr),stat =身份)+
scale_x_date(breaks = extremedates)+
opts(axis.text.x = theme_text(angle = 90,size = 5))


I have a data frame that has dates and cpu utilization data for several months. I can create a smoothed gplot like this:

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

This does not show the dates, it only shows couple of dates. Is it possible to show dates that are of importance like if the date is greater than or smaller than smoothed line?

again, I am sorry if I am asking too many questions. I am just learning R and going through that first pain.

the data looks like this:

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

解决方案

What you want is still not clear, but I'll take a stab at it.

Let's start by making your dataset reproducible.

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

Here, the Date column is of class Date; I don't know if that is what you have or not (can't tell from what you posted; that is why a completely reproducible example was requested).

Converting your qplot syntax to ggplot syntax (and adding points so that I can see what is going on easier):

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

Your comment

This does not show the dates, it only shows couple of dates. Is it possible to show dates that are of importance like if the date is greater than or smaller than smoothed line?

is confusing. On the x-axis, of course only some dates are shown. You wouldn't want every point labeled. And every point would be on one side or the other of the smoothed line. So I am going to interpret your request as labeling the points on the graph that fall outside the confidence interval drawn on the graph. If this isn't what you meant, then you need to give more detail.

In order to do this, you need to not have ggplot2 do the modeling, but rather do it yourself.

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

With this, the original graph can be reproduced.

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

Now with this separate data set, you can further annotate points as to which ones are extreme and should be labeled.

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)

You can do even more formatting of the text to make it nicer. Here is one example.

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)

EDIT

you can just pull out the dates you want on the axis and pass that to the breaks argument of 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))

这篇关于以可读格式显示xaxis上的日期R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆