Search code examples
rplotxts

R: x-axis ends prematurely in an XTS plot


I've got some xts I want to plot. The data is split into month from the beginning of January 2015 to end of April 2019.

           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

I create the plot like this:

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

Though the data seems to be finde, the labeling of the x-axis ends in December 2018:

enter image description here

My best guess is that the dates get split into 6 month periods and because the first half year of 2019 isnt done yet, it just does not display it correctly. So the solution would be, to split the dates displayed on the xaxis into three month periods or so. But this, I cant figure out. To be clear: I dont want to touch the actual data. I just want to display the xaxis correctly.


Solution

  • This is very simple, just use argument major.ticks = 'quarters'. Note that my locale is not an English language locale, so the months names abbreviations are not the same as in the question.

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

    enter image description here

    Data in dput format.

    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")