用ggplot2在x轴绘制朱利安日

时间:2021-12-08 20:11:26

I have data for daily irrigation that crosses from year to the next one:

我有从一年到下一年的每日灌溉数据:

df <- structure(list(date = structure(c(4261, 4262, 4263, 4264, 4265, 
4266, 4267, 4268, 4269, 4270, 4271, 4272, 4273, 4274, 4275, 4276, 
4277, 4278, 4279, 4280, 4281, 4282, 4283, 4284, 4285, 4286, 4287, 
4288, 4289, 4290, 4291, 4292, 4293, 4294, 4295, 4296, 4297, 4298, 
4299, 4300, 4301, 4302, 4303, 4304, 4305, 4306, 4307, 4308, 4309, 
4310, 4311, 4312, 4313, 4314, 4315, 4316, 4317, 4318, 4319, 4320, 
4321, 4322, 4323, 4324, 4325, 4326, 4327, 4328, 4329, 4330, 4331, 
4332, 4333, 4334, 4335, 4336, 4337, 4338, 4339, 4340, 4341, 4342, 
4343, 4344, 4345, 4346, 4347, 4348, 4349, 4350, 4351, 4352, 4353, 
4354, 4355, 4356, 4357, 4358, 4359, 4360, 4361, 4362, 4363, 4364, 
4365, 4366, 4367, 4368, 4369, 4370, 4371, 4372, 4373, 4374, 4375, 
4376, 4377, 4378, 4379, 4380, 4381, 4382, 4383, 4384, 4385, 4386, 
4387, 4388, 4389, 4390, 4391, 4392, 4393, 4394, 4395, 4396, 4397, 
4398, 4399, 4400, 4401, 4402, 4403, 4404, 4405, 4406, 4407, 4408, 
4409, 4410, 4411, 4412, 4413, 4414, 4415, 4416, 4417, 4418, 4419, 
4420, 4421, 4422, 4423, 4424, 4425, 4426, 4427, 4428, 4429, 4430, 
4431, 4432, 4433, 4434, 4435, 4436, 4437, 4438, 4439, 4440, 4441, 
4442, 4443, 4444, 4445, 4446, 4447, 4448, 4449, 4450, 4451, 4452, 
4453, 4454, 4455, 4456, 4457, 4458, 4459, 4460, 4461, 4462, 4463, 
4464, 4465, 4466, 4467, 4468, 4469, 4470, 4471, 4472), class = "Date"), 
    jday = c(244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 
    254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 
    266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 
    278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 
    290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 
    302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 
    314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 
    326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 
    338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 
    350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 
    362, 363, 364, 365, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
    28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 
    43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 
    58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 
    73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 
    88, 89, 90), irrigation = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 68.999106583086, 0.613825511006476, 0.650504269646209, 
    0.675321041959003, 1.07386107583648, 0.704743246430332, 0.707284723670737, 
    0.776293467549444, 0.755267782118714, 1.18487391425568, 0.845114328328846, 
    0.823802762818568, 0.870540767040068, 0.939387145551663, 
    0.980778073801578, 0.961748234276633, 1.00175189046026, 1.2003019943978, 
    1.08581774443099, 1.02293983015042, 1.02830459076224, 0.841190189991182, 
    0.980525840833349, 1.10596434583942, 1.16474588634898, 1.13363473392227, 
    1.3104655997267, 1.04313889290523, 1.12900495066226, 1.14512528725041, 
    1.15993003474856, 1.36629089799899, 1.35725439867927, 1.41586446299136, 
    1.46648144027562, 1.20177115282966, 1.2724354938396, 1.17885413679104, 
    1.50501941940159, 1.61153108170889, 1.59654419167528, 1.57815025848092, 
    1.69451670970732, 1.90409241833733, 1.96138691207738, 2.31347289594632, 
    2.62349860182086, 2.02034176206126, 2.43931099974993, 2.80426669815212, 
    3.64748623301682, 3.61020936317814, 3.56980733038152, 4.2411068481149, 
    3.56775806019607, 4.05131195586862, 4.10309786009557, 3.82752398148324, 
    3.64170968879774, 3.72256845640905, 4.52625782049975, 3.77543871611067, 
    3.83794749361797, 3.57695659155985, 4.00032328633429, 4.00259658665333, 
    4.33779646123497, 3.9441870717169, 3.87438364862238, 3.42668787021081, 
    3.00505732564093, 3.26796355756741, 3.20416667160479, 4.02001638319886, 
    4.12818549443217, 3.56875502947465, 3.40190915228094, 3.97419348040831, 
    3.80111025838019, 4.26330353449849, 3.45396023352169, 3.31773448684841, 
    3.56689567936277, 4.24906589915452, 3.61606846040892, 3.71226018146404, 
    3.91100839040812, 4.01828006633277, 4.16464814862001, 3.7494809419206, 
    3.759374359279, 4.36243338723784, 0, 0.279377742878442, 3.12715397066283, 
    4.96814538900135, 5.27112768228772, 5.37505629456159, 5.80227566691278, 
    5.68143063147091, 5.76655560095333, 6.02031850351871, 5.72975780894455, 
    5.77516228018455, 5.40222501291812, 5.21530269882054, 5.86467400337886, 
    5.47526718806295, 5.20959328216256, 4.8062177676599, 5.08492012394285, 
    5.14194736665892, 5.55457368869226, 4.99883698028268, 5.17854310933826, 
    5.40636266319497, 4.97066477432992, 4.879204407479, 4.66249571494686, 
    4.63900838314908, 4.40660802600453, 4.42912446179436, 4.31494981339834, 
    3.53431485000166, 3.65618335390554, 3.26147690560054, 3.4740653732448, 
    3.21587573671804, 3.68968358086151, 2.59795268530985, 2.81539502190155, 
    2.96434859859133, 2.93298987971926, 2.61560347473737, 3.65341749469053, 
    2.8604916137399, 3.15716915686154, 3.10825421972182, 3.41439628601074, 
    3.00367127575921, 2.73676940769825, 2.89370579395479, 2.99327868859745, 
    2.49745100447275, 2.18776641771631, 2.18157188637743, 2.25322256736385, 
    2.39152978693397, 1.64653250314657, 0.287038117936514, 0.247974432787849, 
    0.245568821731123, 0.225235263120781, 0.239999308169467, 
    0.264739379142095, 0.292488079626583, 0.253238603906724, 
    0.291291301690259, 0.220847472403813, 0.00379066096926198, 
    0.00399085850391573, 0.00137884639999242, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), cumIrri = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68.999106583086, 69.6129320940925, 
    70.2634363637387, 70.9387574056977, 72.0126184815342, 72.7173617279645, 
    73.4246464516353, 74.2009399191847, 74.9562077013034, 76.1410816155591, 
    76.9861959438879, 77.8099987067065, 78.6805394737466, 79.6199266192983, 
    80.6007046930998, 81.5624529273765, 82.5642048178367, 83.7645068122345, 
    84.8503245566655, 85.8732643868159, 86.9015689775782, 87.7427591675694, 
    88.7232850084027, 89.8292493542421, 90.9939952405911, 92.1276299745134, 
    93.4380955742401, 94.4812344671453, 95.6102394178076, 96.755364705058, 
    97.9152947398065, 99.2815856378055, 100.638840036485, 102.054704499476, 
    103.521185939752, 104.722957092581, 105.995392586421, 107.174246723212, 
    108.679266142614, 110.290797224323, 111.887341415998, 113.465491674479, 
    115.160008384186, 117.064100802523, 119.025487714601, 121.338960610547, 
    123.962459212368, 125.982800974429, 128.422111974179, 131.226378672331, 
    134.873864905348, 138.484074268526, 142.053881598908, 146.294988447023, 
    149.862746507219, 153.914058463087, 158.017156323183, 161.844680304666, 
    165.486389993464, 169.208958449873, 173.735216270373, 177.510654986483, 
    181.348602480101, 184.925559071661, 188.925882357995, 192.928478944649, 
    197.266275405884, 201.210462477601, 205.084846126223, 208.511533996434, 
    211.516591322075, 214.784554879642, 217.988721551247, 222.008737934446, 
    226.136923428878, 229.705678458353, 233.107587610634, 237.081781091042, 
    240.882891349422, 245.146194883921, 248.600155117442, 251.917889604291, 
    255.484785283653, 259.733851182808, 263.349919643217, 267.062179824681, 
    270.973188215089, 274.991468281422, 279.156116430042, 282.905597371962, 
    286.664971731241, 291.027405118479, 291.027405118479, 291.306782861358, 
    294.433936832021, 299.402082221022, 304.67320990331, 310.048266197871, 
    315.850541864784, 321.531972496255, 327.298528097208, 333.318846600727, 
    339.048604409671, 344.823766689856, 350.225991702774, 355.441294401595, 
    361.305968404974, 366.781235593037, 371.990828875199, 376.797046642859, 
    381.881966766802, 387.023914133461, 392.578487822153, 397.577324802436, 
    402.755867911774, 408.162230574969, 413.132895349299, 418.012099756778, 
    422.674595471725, 427.313603854874, 431.720211880878, 436.149336342673, 
    440.464286156071, 443.998601006073, 447.654784359978, 450.916261265579, 
    454.390326638824, 457.606202375542, 461.295885956403, 463.893838641713, 
    466.709233663614, 469.673582262206, 472.606572141925, 475.222175616662, 
    478.875593111353, 481.736084725093, 484.893253881954, 488.001508101676, 
    491.415904387687, 494.419575663446, 497.156345071144, 500.050050865099, 
    503.043329553697, 505.540780558169, 507.728546975886, 509.910118862263, 
    512.163341429627, 514.554871216561, 516.201403719708, 516.488441837644, 
    516.736416270432, 516.981985092163, 517.207220355284, 517.447219663453, 
    517.711959042595, 518.004447122222, 518.257685726129, 518.548977027819, 
    518.769824500223, 518.773615161192, 518.777606019696, 518.778984866096, 
    518.778984866096, 518.778984866096, 518.778984866096, 518.778984866096, 
    518.778984866096, 518.778984866096, 518.778984866096, 518.778984866096, 
    518.778984866096, 518.778984866096, 518.778984866096, 518.778984866096, 
    518.778984866096, 518.778984866096, 518.778984866096, 518.778984866096, 
    518.778984866096, 518.778984866096, 518.778984866096, 518.778984866096
    )), .Names = c("date", "jday", "irrigation", "cumIrri"), row.names = 610:821, class = "data.frame")

And I am trying to plot this time series using ggplot2, but showing the julian day (elapsed day since beginning of the year) instead of actual date on the x-axis.

我试着用ggplot2绘制这个时间序列,但是显示朱利安日(从年初开始)而不是x轴上的实际日期。

The problem is that ggplot insists in plotting the data as sequential, even though I need to plot the order of the data "as is".

问题是,ggplot坚持将数据按顺序绘制,即使我需要将数据的顺序绘制成“原样”。

For example, compare this:

例如,比较:

library(ggplot2)
ggplot(df, aes(x=jday)) + geom_line(aes(y=irrigation), size=1)

To what I get if I plot the actual date:

如果我画出实际的日期:

ggplot(df, aes(x=date)) + geom_line(aes(y=irrigation), size=1)

How can I overcome this?

我如何克服这个?

1 个解决方案

#1


2  

Based on a reply I got from the ggplot2 discussion list, this is the answer that yields what I needed:

根据我从ggplot2讨论列表中得到的回复,这是我需要的答案:

library(scales)    
ggplot(df, aes(x=date)) + geom_line(aes(y=irrigation), size=1) + scale_x_date(labels=date_format('%j'))

As easy as plotting dates and formatting them as julian days using the scale's scale_x_date.

简单到绘制日期并使用scale的scale_x_date将其格式化为julian days。

#1


2  

Based on a reply I got from the ggplot2 discussion list, this is the answer that yields what I needed:

根据我从ggplot2讨论列表中得到的回复,这是我需要的答案:

library(scales)    
ggplot(df, aes(x=date)) + geom_line(aes(y=irrigation), size=1) + scale_x_date(labels=date_format('%j'))

As easy as plotting dates and formatting them as julian days using the scale's scale_x_date.

简单到绘制日期并使用scale的scale_x_date将其格式化为julian days。