如何对时间序列图进行去字母排序?

时间:2014-04-19 19:55:00

标签: r ggplot2 time-series

下面是我的对象(obj)和我的数据图表。我一路搞砸,试图按月计划。我喜欢它的情节,除了我希望x轴按时间顺序排列按月保持分组。

obj <- structure(list(date = structure(c(1341129600, 1341216000, 1341302400, 
1341388800, 1341475200, 1341561600, 1341648000, 1341734400, 1341820800, 
1341907200, 1341993600, 1342080000, 1342166400, 1342252800, 1342339200, 
1342425600, 1342512000, 1342598400, 1342684800, 1342771200, 1342857600, 
1342944000, 1343030400, 1343116800, 1343203200, 1343289600, 1343376000, 
1343462400, 1343548800, 1343635200, 1343721600, 1343808000, 1343894400, 
1343980800, 1344067200, 1344153600, 1344240000, 1344326400, 1344412800, 
1344499200, 1344585600, 1344672000, 1344758400, 1344844800, 1344931200, 
1345017600, 1345104000, 1309507200, 1309593600, 1309680000, 1309766400, 
1309852800, 1309939200, 1310025600, 1310112000, 1310198400, 1310284800, 
1310371200, 1310457600, 1310544000, 1310630400, 1310716800, 1310803200, 
1310889600, 1310976000, 1311062400, 1311148800, 1311235200, 1311321600, 
1311408000, 1311494400, 1311580800, 1311667200, 1311753600, 1311840000, 
1311926400, 1312012800, 1312099200, 1312185600, 1312272000, 1312358400, 
1312444800, 1312531200, 1312617600, 1312704000, 1312790400, 1312876800, 
1312963200, 1313049600, 1313136000, 1313222400, 1277971200, 1278057600, 
1278144000, 1278230400, 1278316800, 1278403200, 1278489600, 1278576000, 
1278662400), class = c("POSIXct", "POSIXt"), tzone = ""), n = c(3970L, 
8970L, 7067L, 5514L, 4913L, 3426L, 3648L, 5466L, 6470L, 6774L, 
12054L, 9726L, 10548L, 20214L, 119274L, 196356L, 72726L, 31606L, 
28722L, 40230L, 97914L, 110898L, 88255L, 51222L, 61420L, 61812L, 
65250L, 63438L, 69870L, 43494L, 40920L, 24876L, 25284L, 18102L, 
16908L, 22080L, 14604L, 10278L, 10764L, 11118L, 13968L, 9560L, 
10309L, 8273L, 13338L, 13709L, 6217L, 2256L, 4260L, 3084L, 2244L, 
4272L, 4647L, 5302L, 4737L, 6522L, 6846L, 3510L, 3102L, 3822L, 
6400L, 2916L, 27826L, 230643L, 177053L, 87978L, 113178L, 90426L, 
37974L, 106313L, 110772L, 79518L, 77982L, 73092L, 55470L, 36540L, 
30384L, 18240L, 21714L, 20707L, 10396L, 10074L, 11220L, 22086L, 
17316L, 6114L, 12198L, 16524L, 11326L, 12204L, 10029L, 5094L, 
5562L, 3738L, 7437L, 13122L, 14768L, 6115L, 10338L, 7332L), year = c(2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L
)), .Names = c("date", "n", "year"), row.names = c(NA, 100L), class = "data.frame")

和绘图

ggplot(data= obj[1:100,], aes(x = format(date, "%b-%d"), y = n, color = year, group = year)) +   geom_line() +
  theme(plot.title = element_text(lineheight=.9, face="bold")) 

2 个答案:

答案 0 :(得分:1)

试试这个:

library(ggplot2)
library(scales)

date12 <- as.Date(sub("....", 2012, as.Date(obj$date)))

ggplot(data= obj[1:100,], aes(date12, n, color = year, group = year)) +   
   geom_line() +
   theme(plot.title = element_text(lineheight=.9, face="bold")) + 
   scale_x_date(labels = date_format("%b-%d")) + 
   xlab("")

screenshot

或者这就是你要找的东西:

library(scales)
ggplot(data= obj[1:100,], aes(as.Date(date), n, color = year, group = year)) +   
   geom_line() +
   theme(plot.title = element_text(lineheight=.9, face="bold")) + 
   scale_x_date(labels = date_format("%b-%d")) + 
   xlab("")

虽然在这种情况下您可能想尝试与"%b-%d"不同的其他内容。

screenshot

ADDED 稍后添加了第一个解决方案。

答案 1 :(得分:0)

 ggplot(data= obj[1:100,], aes(x = as.POSIXlt(date)$yday, y = n, color = year, group = year)) + 
         geom_line() + 
         scale_x_discrete(limits=c('182', '228'), 
                          labels=format.Date(as.Date(182:228,origin='1970-01-01'),"%b-%d") )+
         theme(plot.title = element_text(lineheight=.9, face="bold"))

我无法在正在发生的各种隐式强制步骤之间获得正确的限制。使用xlim()修复但仍然会发出警告。

enter image description here

 png(); ggplot(data= obj[1:100,], aes(x = as.POSIXlt(date)$yday, y = n, color = year, group = year)) +   geom_line() + scale_x_discrete(labels=format.Date(as.Date(182:228,origin='1970-01-01'),"%b-%d") )+xlim(181,228)+
  theme(plot.title = element_text(lineheight=.9, face="bold")); dev.off()

enter image description here