R:x轴在XTS图中过早终止

时间:2019-05-07 11:25:02

标签: r plot xts

我有一些要绘制的xts。数据从2015年1月开始到2019年4月结束按月划分。

           migranti immigrati rifugiati immigrazione
2015-01-01       34        42        22           43
2015-02-01       54        44        27           66
2015-03-01       41        58        44           54
2015-04-01      121        62        43          100
2015-05-01      112        57        52           98
2015-06-01      154       101        60          125
2015-07-01       78        53        35           66
2015-08-01      146        72        58          108
2015-09-01      282        79       157          134
2015-10-01      143        40        89           72
2015-11-01      102        45        90           69
2015-12-01      106        47        74           62
2016-01-01      177        83       116          103
2016-02-01      151        65        86           86
2016-03-01      161        71        91           81
2016-04-01      180        55       102           81
2016-05-01      210        72        99          119
2016-06-01      115        71        78          125
2016-07-01       91        59        55           69
2016-08-01      114        40        54           50
2016-09-01      159        55        84          101
2016-10-01      193        77        72          127
2016-11-01      128        86        61          108
2016-12-01      115        47        52           75
2017-01-01      132       100        77          125
2017-02-01      123        96        58          122
2017-03-01      137        87        61          112
2017-04-01      139        51        63           78
2017-05-01      207        67        71          121
2017-06-01      134        77        58           97
2017-07-01      184        60        53          107
2017-08-01      199        59        49           92
2017-09-01      154        88        67          101
2017-10-01      125        53        50           96
2017-11-01      127        59        51           73
2017-12-01      103        38        31           53
2018-01-01       96        48        43           57
2018-02-01       87        60        41           69
2018-03-01       69        33        30           54
2018-04-01       69        29        33           47
2018-05-01      101        39        41           74
2018-06-01      326        72        86          181
2018-07-01      212        58        49          112
2018-08-01      198        59        38           70
2018-09-01      147        46        31           84
2018-10-01      181        71        38          107
2018-11-01      117        35        34           71
2018-12-01      118        42        38           57
2019-01-01      215        49        44           88
2019-02-01      132        45        26           67
2019-03-01      119        49        40           84
2019-04-01      110        28        37           72

我这样创建情节:

plot(
  sum_data, main = "Data",
  xlab = "Januar 2015 - April 2019",
  lwd = 8,
  cex = 3,
  las = 1,
  )

尽管可以找到数据,但x轴的标签在2018年12月结束:

enter image description here

我最好的猜测是,日期被划分为6个月,并且由于尚未完成2019年的上半年,因此它无法正确显示。因此,解决方案是将x轴上显示的日期分为三个月左右。但是,我不知道。需要明确的是:我不想触及实际数据。我只想正确显示xaxis。

1 个答案:

答案 0 :(得分:1)

这非常简单,只需使用参数major.ticks = 'quarters'。请注意,我的语言环境不是英语语言环境,因此月份名称的缩写与问题中的名称不同。

plot(sum_data, major.ticks = 'quarters')

enter image description here

dput格式的数据。

sum_data <-
structure(c(34L, 54L, 41L, 121L, 112L, 154L, 78L, 146L, 282L, 
143L, 102L, 106L, 177L, 151L, 161L, 180L, 210L, 115L, 91L, 114L, 
159L, 193L, 128L, 115L, 132L, 123L, 137L, 139L, 207L, 134L, 184L, 
199L, 154L, 125L, 127L, 103L, 96L, 87L, 69L, 69L, 101L, 326L, 
212L, 198L, 147L, 181L, 117L, 118L, 215L, 132L, 119L, 110L, 42L, 
44L, 58L, 62L, 57L, 101L, 53L, 72L, 79L, 40L, 45L, 47L, 83L, 
65L, 71L, 55L, 72L, 71L, 59L, 40L, 55L, 77L, 86L, 47L, 100L, 
96L, 87L, 51L, 67L, 77L, 60L, 59L, 88L, 53L, 59L, 38L, 48L, 60L, 
33L, 29L, 39L, 72L, 58L, 59L, 46L, 71L, 35L, 42L, 49L, 45L, 49L, 
28L, 22L, 27L, 44L, 43L, 52L, 60L, 35L, 58L, 157L, 89L, 90L, 
74L, 116L, 86L, 91L, 102L, 99L, 78L, 55L, 54L, 84L, 72L, 61L, 
52L, 77L, 58L, 61L, 63L, 71L, 58L, 53L, 49L, 67L, 50L, 51L, 31L, 
43L, 41L, 30L, 33L, 41L, 86L, 49L, 38L, 31L, 38L, 34L, 38L, 44L, 
26L, 40L, 37L, 43L, 66L, 54L, 100L, 98L, 125L, 66L, 108L, 134L, 
72L, 69L, 62L, 103L, 86L, 81L, 81L, 119L, 125L, 69L, 50L, 101L, 
127L, 108L, 75L, 125L, 122L, 112L, 78L, 121L, 97L, 107L, 92L, 
101L, 96L, 73L, 53L, 57L, 69L, 54L, 47L, 74L, 181L, 112L, 70L, 
84L, 107L, 71L, 57L, 88L, 67L, 84L, 72L), .Dim = c(52L, 4L), 
.Dimnames = list(NULL, c("migranti", "immigrati", "rifugiati", 
"immigrazione")), index = structure(c(1420070400, 1422748800, 1425168000, 
1427846400, 1430438400, 1433116800, 1435708800, 1438387200, 1441065600, 
1443657600, 1446336000, 1448928000, 1451606400, 1454284800, 1456790400, 
1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 1472688000, 
1475280000, 1477958400, 1480550400, 1483228800, 1485907200, 1488326400, 
1491004800, 1493596800, 1496275200, 1498867200, 1501545600, 1504224000, 
1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 1519862400, 
1522540800, 1525132800, 1527811200, 1530403200, 1533081600, 1535760000, 
1538352000, 1541030400, 1543622400, 1546300800, 1548979200, 1551398400, 
1554076800), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"), .indexCLASS = "Date", tclass = "Date", 
.indexTZ = "UTC", tzone = "UTC")