我正在R中绘制时间序列,我的数据有时会在年中开始和结束。 R在我的情节的x轴上自动离开了领先和滞后的空间,你们帮助我在这篇帖子中消除了这个:
然而,我只是注意到这段代码也遗漏了对应于第一个和最后一个观察结果的条形码:
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
答案 0 :(得分:0)
设置yAxis: {
labels: {
formatter: function() {
var chart = this.chart,
yAxis = chart.yAxis[0];
maxDataValue = ((yAxis.dataMax * 8) / 300) / 1024;
if (maxDataValue < 1000) {
yAxis.titleText = "Mbps";
return Math.floor(((this.value * 8) / 300) / 1024) + " Mbps";
} else {
yAxis.titleText = "Gbps";
return Math.floor(((this.value * 8) / 300) / 1048576) + " Gbps";
}
}
}
},
function(chart) {
var yAxis = chart.yAxis[0],
titleText = yAxis.titleText;
yAxis.setTitle({
text: titleText
});
}
并使用Year
在x轴上占用了太多空间,边缘条不再适合。从limits
移除expand = c(0, 0)
可以解决此问题。
使用
limits
而不是
scale_x_date