R ggplot2:时间序列的条形图

时间:2016-06-19 22:04:05

标签: r ggplot2

我有一个文件显示过去十年中股票的月回报率以及整个投资组合的总回报率。我想描绘这些变化,但我有两个问题。首先,R正在按字母顺序排列我的数据'按日期排序,而不是日期的时间顺序(因为它们在文件中排序)。其次,日期轴太拥挤,我想每隔十二个月才会显示一个标签来清理它。我知道我的行标题实际上是字符而不是日期,但是当我尝试使用as.Date()函数转换它们时,我会遇到一些其他奇怪的问题。

这是我的代码:

library(ggplot2)
library(scales)


wbRed <- rgb(red=187, green=8, blue=38, maxColorValue = 255)

sample<-read.csv(file="C:/Users/Desktop/sample.csv", header=TRUE, sep=",", row.names=1)

ggplot(data=sample, aes(x=rownames(sample), y=Total)) + geom_bar(stat="identity", fill=bRed, color=wfRed) +
    theme_bw() + theme(panel.border = element_blank(), panel.grid.major.x = element_blank(), 
    panel.grid.major.y = element_line(colour = "black"), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
    labs(x="", y="Changes in Value") + 
    #scale_x_date(breaks="12 months") +
    ggtitle("Historical Simulation Results") +
    guides(fill=FALSE) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

以下是数据:

> sample
                  GBP          ZAR          EUR          SGD           HKD        Total
7/1/2006  -0.58644258  2.659271979  0.016035567  0.013142789  2.203442e-03  2.104211198
8/1/2006   0.94918267 -1.093805058 -0.043216559 -0.038509824  1.671150e-03 -0.224677625
9/1/2006   0.96858284  1.207307075 -0.009851115 -0.070232623  1.457896e-03  2.097264071
10/1/2006 -1.08921652  2.444999289  0.196899486  0.160219941  3.651512e-03  1.716553704
11/1/2006  1.22933459 -1.733890276 -0.103431071 -0.274692091 -4.029816e-03 -0.886708668
12/1/2006  2.39175673 -0.709273288 -0.718068825 -0.190843849 -5.834339e-04  0.772987340
1/1/2007  -0.70139084 -1.037347291  0.161052471 -0.091796714  8.747750e-04 -1.668607603
2/1/2007   0.24392307  0.667446624  0.214157276  0.004171487  7.264080e-03  1.136962537
3/1/2007  -0.26855741  0.506969991 -0.198908248 -0.075440834  1.979220e-03 -0.033957285
4/1/2007   0.30181757 -0.055464872 -0.201225421 -0.111841970  2.638671e-04 -0.066450831
5/1/2007   0.98740559 -1.103129028 -0.295675453  0.039994089  2.187714e-03 -0.369217087
6/1/2007  -0.51281185  0.284122261  0.179949941  0.111827891 -3.194268e-03  0.059893977
7/1/2007   0.83422369 -0.220374802 -0.108039973 -0.008365349  1.978028e-03  0.499421593
8/1/2007   0.74504059  0.272036764 -0.146962470 -0.137101361  3.159977e-03  0.736173504
9/1/2007  -0.47644001  0.212532644  0.042757782  0.086014485 -8.381567e-03 -0.143516667
10/1/2007  0.82299000 -1.498736409 -0.696410404 -0.520975027 -7.429962e-03 -1.900561805
11/1/2007  1.12622328 -1.199285692 -0.215304420 -0.303504111 -3.535271e-03 -0.595406211
12/1/2007 -0.71946687  1.000826869 -0.227737289 -0.016571920  8.101840e-03  0.045152628
1/1/2008  -2.13751297  0.245695317  0.044252429 -0.091108927  3.489005e-03 -1.935185146
2/1/2008  -0.67426553  2.266769351 -0.227295641 -0.299956694 -1.084288e-03  1.064167202
3/1/2008   0.76797069  1.982247416 -0.402260681 -0.209856432 -3.921403e-03  2.134179588
4/1/2008  -0.41728750  0.636163043 -0.452618947 -0.202055162  1.826603e-03 -0.433971960
5/1/2008  -0.03500627 -1.625282529  0.141612143 -0.192736609  1.772133e-03 -1.709641127
6/1/2008   0.23881203  0.003037533 -0.081653354  0.007048234  2.456902e-03  0.169701346
7/1/2008   0.39968626  1.158605611 -0.242684724 -0.012932209 -1.136607e-03  1.301538334
8/1/2008  -0.62726348 -3.048904898  0.229011600  0.131755170  1.294386e-03 -3.314107223
9/1/2008  -5.52714272  2.279766382  0.960982305  0.580924888  1.848951e-04 -1.705284253
10/1/2008 -1.10654011  1.947384995  0.656949470  0.152472655 -9.872998e-03  1.640394008
11/1/2008 -5.75972068  5.194938622  1.446459091  0.493990493 -4.895018e-03  1.370772512
12/1/2008 -4.66247285  2.279102044  0.142722537  0.485284557  3.457778e-04 -1.755017939
1/1/2009  -0.87029325 -3.588719911 -1.795916993 -1.060544288 -2.926180e-04 -7.315767064
2/1/2009  -0.59119433  2.345077104  1.385401324  0.793838794  1.169842e-03  3.934292733
3/1/2009  -0.96007824 -0.271264076  0.177499977  0.383779342  1.861018e-04 -0.669876894
4/1/2009   0.65875960 -2.667513309 -0.723056595 -0.294984213 -1.409976e-03 -3.028204489
5/1/2009   1.96882792 -3.127963575 -0.028609797 -0.403961403  7.981331e-05 -1.591627038
6/1/2009   6.41408603 -2.231438902 -1.054268575 -0.439470734  1.063899e-04  2.689014205
7/1/2009   0.13384589 -1.156499819  0.018962822  0.023271366 -1.330022e-04 -0.980552741
8/1/2009   0.89677072  0.096589612 -0.128432117 -0.053369099 -2.660512e-05  0.811532516
9/1/2009  -2.08060087  0.634342994  0.036557158  0.054296114  2.393951e-04 -1.355165213
10/1/2009 -0.79768342 -0.874763535 -0.356426448 -0.342945092 -2.660307e-04 -2.372084522
11/1/2009  1.96268495  0.448763238 -0.188939237 -0.153116719  5.320887e-05  2.069445442
12/1/2009  0.60385136 -2.329732947 -0.388433698 -0.230667586 -2.660512e-05 -2.345009478
1/1/2010  -1.74132611  0.403503616  0.792779431  0.265595426  1.355853e-03 -0.278091783
2/1/2010  -0.75140823  0.456099846  0.433326124  0.072622163  3.423850e-03  0.214063748
3/1/2010  -3.81036055  0.640417192  0.420608700 -0.048942253 -9.293740e-04 -2.799206281
4/1/2010   1.26256559 -1.887625782 -0.033777301 -0.100791765  4.778449e-04 -0.759151411
5/1/2010  -0.07401611  0.715976278  0.342863419 -0.308199151 -6.107630e-04  0.676013671
6/1/2010  -2.56479890  1.430223048  1.265262299  0.479035956  7.065900e-03  0.616788302
7/1/2010   2.25754561  0.069547202 -0.384868133 -0.218102548  9.787040e-04  1.725100840
8/1/2010   2.12129997 -2.002981468 -0.661909875 -0.396428726 -7.353183e-03 -0.947373284
9/1/2010  -0.93786138  0.027130548  0.294046384 -0.161598738  2.386347e-03 -0.775896840
10/1/2010  1.50132742 -1.773883748 -1.210829229 -0.414460710 -4.783787e-03 -1.902630060
11/1/2010  0.84243741  0.208322569 -0.116812965 -0.258893369 -1.303074e-03  0.673750572
12/1/2010 -1.61946685  0.234111714  0.857160095  0.224724364  4.113754e-03 -0.299356923
1/1/2011  -0.04427375 -1.970713448 -0.294503463 -0.334150932  1.246634e-03 -2.642394959
2/1/2011   2.13469280  2.282033236 -0.525122609 -0.157386901  4.868956e-03  3.739085482
3/1/2011   0.48693470 -0.575616396  0.059388069  0.033923510 -1.852524e-04  0.004444635
4/1/2011  -0.60302482 -1.486048101 -0.527338898 -0.170151292 -3.313314e-03 -2.789876428
5/1/2011   2.32617198 -0.649548334 -0.632328861 -0.467874781 -3.212305e-03  0.573207701
6/1/2011  -1.40387493  1.288972624  0.510922550  0.191113006  3.551294e-03  0.590684548
7/1/2011  -1.00091882 -0.513200170 -0.218256030 -0.164375663  4.769235e-04 -1.896273759
8/1/2011   0.86845450 -0.020128235  0.300088724 -0.332927768  2.487622e-03  0.817974840
9/1/2011  -0.45146377  1.315599967 -0.009975032  0.037201219 -2.225383e-03  0.889137001
10/1/2011 -2.31250856  4.449371664  0.965859710  1.259534419  8.474239e-04  4.363104656
11/1/2011  1.47276001  0.017528935 -0.372812490 -0.402806664 -3.820433e-03  0.710849358
12/1/2011 -1.02113783 -0.061260527  0.278924294  0.083635478 -5.042073e-04 -0.720342789
1/1/2012  -0.58913178  0.016328180  0.586650323  0.181418366 -5.043403e-04  0.194760751
2/1/2012   1.17727033 -1.724962152 -0.243712676 -0.584606347 -3.190171e-03 -1.379201013
3/1/2012   0.48846396 -1.101179431 -0.180006838 -0.011521333  0.000000e+00 -0.804243639
4/1/2012   0.20097266  1.001504747 -0.037968718  0.097967845  2.840647e-03  1.265317186
5/1/2012   0.83668231  0.266816874  0.125469744 -0.283573627 -2.099440e-03  0.943295862
6/1/2012  -3.32604195  3.234061989  0.958103906  0.703159921  6.110459e-04  1.569894916
7/1/2012   1.40799292 -1.662859673 -0.295959096 -0.347838597 -8.239189e-04 -0.899488367
8/1/2012  -0.68457483  0.731339343  0.551105992 -0.224456715 -5.316851e-04  0.372882108
9/1/2012   1.33565036  0.200106161 -0.457341939 -0.002565620  5.316600e-05  1.075902128
10/1/2012  1.04629593 -0.068942477 -0.387970761 -0.236929300 -2.924765e-04  0.352160914
11/1/2012  0.00000000  0.992360534 -0.067400605 -0.120669610 -1.170523e-03  0.803119795
12/1/2012 -0.45610996  0.988240667 -0.052471096  0.002622814  5.320956e-05  0.482335637
1/1/2013   0.92673882 -1.760102676 -0.265135407  0.011794013  1.063899e-04 -1.086598856
2/1/2013  -2.15162316  1.426314409 -0.521515965  0.251520132  1.727623e-03 -0.993576965
3/1/2013  -2.62454113  0.817551566  0.715584194 -0.002580109 -6.380921e-04 -1.094623575
4/1/2013   0.79865892  0.461617034  0.209823944 -0.003871108  2.177831e-03  1.468406618
5/1/2013   1.34605954 -0.628173666 -0.406860285 -0.094754555 -7.173479e-04  0.215553690
6/1/2013  -1.44316590  3.502774566  0.216895129  0.398685393  6.108913e-04  2.675800078
7/1/2013   0.07861134 -0.515809084 -0.078975075 -0.005064276 -1.807674e-03 -0.523044765
8/1/2013  -0.40083049  0.122513722 -0.172880695  0.159191121  1.594902e-04 -0.291846854
9/1/2013   1.59697270  0.984929294 -0.017937987 -0.021337463 -3.722100e-04  2.542254333
10/1/2013  2.80254340 -0.510847741 -0.363130782 -0.295288780 -2.658962e-04  1.633010196
11/1/2013 -1.04445416  0.215515238  0.045538829 -0.113287367 -5.053295e-04 -0.897192794
12/1/2013  1.74515343 -0.050634442 -0.121788033  0.160579141  1.861536e-04  1.733496255
1/1/2014   0.76065484  1.039290115 -0.199877389  0.093755845  3.456629e-04  1.694169073
2/1/2014  -0.48206336  1.828661146  0.317872746  0.171712389  2.894159e-03  1.839077082
3/1/2014   1.17048025 -1.122263101 -0.370075693 -0.112333469 -1.275274e-03 -0.435467289
4/1/2014  -0.43184784 -0.539561075  0.010298812 -0.106729774 -1.010091e-03 -1.068849963
5/1/2014   0.99444681 -0.302325729 -0.088169550 -0.084300872 -9.839936e-04  0.518666663
6/1/2014  -0.51367729  0.268939878  0.267595025  0.017862051  5.319544e-05  0.040772863
7/1/2014   1.48241814  0.297165149 -0.050966425 -0.107894898 -7.448833e-04  1.619977087
8/1/2014  -1.20628473  0.031234879  0.290959690  0.012834332 -2.660478e-05 -0.871282438
9/1/2014  -0.79624341 -0.011446599  0.351705236  0.037133176  5.320887e-05 -0.418798389
10/1/2014 -1.59776599  1.732689632  0.607546389  0.280535401  3.823675e-03  1.026829105
11/1/2014 -0.74201364 -0.676950833  0.122616842  0.168047328 -2.552274e-03 -1.130852578
12/1/2014 -1.04178881 -0.143308159  0.069354012  0.259615063  1.329161e-04 -0.855994977
1/1/2015  -0.57564114  1.590545247  0.463554996  0.203121207 -7.975802e-05  1.681500548
2/1/2015  -2.12211605  0.282386702  1.060836038  0.362765100 -8.776880e-04 -0.417005902
3/1/2015   1.57828303  0.030874937  0.132885573  0.102146896  9.570355e-04  1.845147470
4/1/2015  -2.50496774  0.907139404  0.610817738  0.003521528 -7.978360e-04 -0.984286908
5/1/2015   1.37868583  0.213481519 -0.639793440 -0.396978450 -1.063881e-04  0.555289067
6/1/2015   0.22000808  0.532370454  0.383597964  0.309088931  1.010156e-03  1.446075585
7/1/2015   1.72083500 -0.032884981 -0.182119411 -0.040211575 -9.042160e-04  1.464714815
8/1/2015   0.02013216  1.118505967  0.098595151  0.223899815  1.063722e-04  1.461239468
9/1/2015  -1.27999580  1.880259019 -0.475942079  0.458748868 -7.448833e-04  0.582325123
10/1/2015 -0.71081929  1.128366674  0.167499426  0.201323203  0.000000e+00  0.786370013
11/1/2015  1.23426063 -0.238195428  0.266639410 -0.336892864  1.330079e-04  0.925944756
12/1/2015 -1.41021239  1.408472554  0.535261439  0.056897460  4.255281e-04  0.590844588
1/1/2016  -1.40087182  2.375901482 -0.331234590  0.067986855 -5.054360e-04  0.711276491
2/1/2016  -1.33471385  0.709930598 -0.046555048  0.116982385  7.395425e-03 -0.546960488
3/1/2016  -2.09559099 -0.665112572  0.029011389 -0.251349765 -1.724403e-03 -2.984766345
4/1/2016   1.23940797 -1.986660024 -0.760043927 -0.589940498 -4.626415e-03 -2.101862893
5/1/2016   1.70163126 -1.092565033 -0.083190940 -0.078568774  7.707896e-04  0.448077306
6/1/2016  -0.84345995  2.903763064  0.362742939  0.381162209  3.634855e-03  2.807843117

enter image description here

3 个答案:

答案 0 :(得分:1)

我建议使用 lubridate 包将rownames转换为日期。以下代码生成您想要的绘图,您无需显式更改日期间距:

library(lubridate)
sample$date <- mdy(row.names(sample))

ggplot(data=sample, aes(x=date, y=Total)) + geom_bar(stat="identity", position = "identity", fill="red") +
        theme_bw() + theme(panel.border = element_blank(), panel.grid.major.x = element_blank(), 
                           panel.grid.major.y = element_line(colour = "black"), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
        labs(x="", y="Changes in Value") + 
        #scale_x_date(breaks="12 months") +
        ggtitle("Historical Simulation Results") +
        guides(fill=FALSE) +
        theme(axis.text.x = element_text(angle = 45, hjust = 1))

这正是您的代码,唯一的区别是 x 值,我简化了您设置填充颜色的部分,因为我没有您使用的变量。结果如下:

enter image description here

答案 1 :(得分:1)

您只需使用x格式化传递给as.Date的日期,这需要一种格式,以便知道如何解析它们。清理了一下:

library(ggplot2)

ggplot(data = sample, aes(x = as.Date(rownames(sample), "%m/%d/%Y"), y = Total)) + 
    geom_bar(stat = "identity", 
             position = 'dodge',
             fill = rgb(187, 8, 38, maxColorValue = 255), 
             color = rgb(187, 8, 38, maxColorValue = 255)) + 
    theme_bw() + 
    theme(panel.border = element_blank(), 
          panel.grid.major.x = element_blank(), 
          panel.grid.major.y = element_line(colour = "black"), 
          panel.grid.minor = element_blank(), 
          axis.line = element_line(colour = "black"), 
          axis.text.x = element_text(angle = 45, hjust = 1)) + 
    labs(x = "", y = "Changes in Value", title = "Historical Simulation Results")

plot with proper x-axis

如果您愿意,请使用scale_x_date格式化轴。

答案 2 :(得分:0)

你觉得这样吗? Í跳过了回答这个问题所不需要的一切。

library(ggplot2)
data$date <- strptime(data$date, format = "%m/%d/%Y" )
ggplot(data = data, aes(x = date, y = Total)) + 
  geom_bar(stat = "identity", width = 2000000)

enter image description here