【问题标题】:R ggplot2: time series bar chart dropping first and last observationsR ggplot2:时间序列条形图删除第一个和最后一个观察值
【发布时间】:2016-09-02 19:38:28
【问题描述】:

我在 R 中绘制时间序列,我的数据有时在年中开始和结束。 R 会自动在我的绘图的 x 轴上留下领先和落后的空间,你们帮助我在这篇文章中消除了这一点:

http://stackoverflow.com/questions/38334655/r-ggplot-time-series-bbar-chart-has-leading-and-lagging-spaces

但是,我只是注意到这段代码也省略了与第一个和最后一个观察对应的条:

ggplot(data=obs, aes(x=as.Date(rownames(obs), '%m/%d/%Y'), y=Portfolio)) + 
    geom_bar(stat="identity", fill=wfRed, 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=parentCCY) + 
    scale_y_continuous(labels=format_si()) +
    scale_x_date(limits = c(min, max), breaks=date_breaks("12 months"), labels=date_format("%b %Y"), expand = c(0,0)) +
    guides(fill=FALSE) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
    geom_hline(yintercept = standalones[,"Portfolio"], linetype=2, size=1.5)

这里是数据样本:

    > obs
                     A         B          C         D         E        F        Portfolio
10/1/2006 -135.265197  -4.928804 -140.162303  -3132.3385  -7087.2885  -668.62304 -11168.6063
11/1/2006  190.727890   4.104793 -141.391549   1645.4138   7999.0054   706.10084  10403.9612
12/1/2006   89.269095  -3.700356 -125.053082  11423.2631  15562.6265   998.39977  27944.8050
1/1/2007     3.131604   5.070734 -203.056183  -2562.0730  -4563.7935 -2186.25627  -9506.9766
2/1/2007   -95.766379   6.107738 -120.531122  -3406.8808   1587.1529 -1068.03237  -3097.9500
3/1/2007    67.659308  -3.094623   57.689483   3164.2945  -1747.4431  1972.28356   3511.3892
4/1/2007   150.491858  10.278402  180.727469   3201.1568   1963.8594  -129.19683   5377.3172
5/1/2007   115.490649   4.361355  437.381619   4703.6974   6424.8275 -1233.72962  10452.0289
6/1/2007    33.437884  24.244469  532.429923  -2862.6999  -3336.7521 -1324.00829  -6933.3479
7/1/2007    96.673897  -4.452097  -47.621030   1718.7336   5428.1071  -629.76578   6561.6757
8/1/2007    37.811147   8.899346   98.206673   2337.9249   4847.8126  2565.55940   9896.2140
9/1/2007  -215.330010 -14.512752    5.339857   -680.2041  -3100.0887  1997.53581  -2007.2599
10/1/2007  456.922869  30.119571  726.404928  11078.7142   5355.0120    18.79104  17665.9645
11/1/2007   99.450743  12.498947  482.472014   3425.1299   7328.0832   676.45319  12024.0880
12/1/2007 -151.107244  -8.886944 -538.445274   3622.9159  -4681.4101  2235.28660    478.3529
1/1/2008   -46.371176   2.482456   69.280944   -703.9815 -13908.3192  -266.20754 -14853.1159
2/1/2008   159.042863   7.064167  -30.582150   3615.8900  -4387.2951  3512.70051   2876.8204
3/1/2008   145.340906  11.529238   85.595411   6399.2885   4997.0136  1921.70517  13560.4729
4/1/2008  -125.795200 -10.970036 -369.785356   7200.4036  -2715.1965  1345.24519   5323.9017
5/1/2008   143.787006  17.532562   14.368170  -2252.8102   -227.7779 -1790.99859  -4095.8990
6/1/2008   118.551194   7.797265  301.867049   1298.9671   1553.8965  -748.84603   2532.2330
7/1/2008    -5.168456   6.466974 -317.625115   3860.7044   2600.6692  -416.67061   5728.3764
8/1/2008  -133.485807   8.680382  -54.874843  -3643.1880  -4081.4633 -1063.40901  -8967.7406
9/1/2008  -419.020984 -19.376143 -417.458238 -15287.6066 -35963.8824  -288.25916 -52395.6035

以及缺少第一个和最后一个观察值的结果图:

【问题讨论】:

  • 您的情节目前无法重现。但是,我认为问题归结为您将 x 轴的限制设置为(可能)数据集的最小值和最大值,然后使用 expand = c(0, 0)。您已经占用了太多空间,边缘条不再适合情节。尝试从 scale_x_date 中删除 limits 参数,情况应该会更好。
  • 是的,成功了,谢谢!

标签: r ggplot2


【解决方案1】:

设置limits 和使用expand = c(0, 0) 在x 轴上占用了太多空间,以至于边缘条不再适合。从scale_x_date 中删除limits 可以解决这个问题。

使用

scale_x_date(breaks=date_breaks("12 months"), labels=date_format("%b %Y"), expand = c(0,0))

代替

scale_x_date(limits = c(min, max), breaks=date_breaks("12 months"), labels=date_format("%b %Y"), expand = c(0,0))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多