我确定这会被标记为重复,但我似乎无法找到解决方案。
我有一个由大气中的氮测量的数据集。每年11月至2月(约20年)每天收集测量结果。日期是朱利安格式。 ggplot2中的默认规格将2月视为应该是11月的第一个月。我如何解决这个问题,同时要记住x轴应该是真的(例如,11月 - 12月 - 1月 - 2月)。提前谢谢。
structure(list(nitro = c(36, 51, 23, 7, 26, 62, 45, 25, 31, 70,
23, 63, 41, 61, 6, 24, 2, 6, 47, 22, 6, 48, 16, 25, 61, 9, 63,
3, 22, 27, 52, 9, 34, 48, 31, 16, 51, 63, 21, 42, 4, 9, 20, 28,
23, 0, 23, 70, 47, 2, 52.0898926784594, 55.8022284640568, 41.3593089128863,
57.9928202206287, 35.4624211011211, 65.2484486428607, 39.236626265918,
40.8906848288849, 65.7044590718657, 39.9377208120021, 51.1368297792738,
46.3981830973577, 36.3795615108741, 59.4071137999101, 42.77053802297,
51.4293854584156, 45.3589706700315, 53.9652146584217, 54.6980728446628,
43.1278297603877, 48.2879709803009, 60.0627232974633, 49.5233404192228,
55.4231572422576, 35.9842318889366, 55.649768270307, 61.8309485366259,
63.1600858734154, 52.9116478602541, 48.5690637010561, 49.4065375720883,
44.7880773761964, 48.5893427534388, 43.5882970429564, 33.0365374509187,
40.2149387649389, 67.3129711280128, 55.491540487927, 63.5614232930341,
53.6421935293565, 47.0292298358771, 62.5727076637489, 32.2728901281264,
61.7219468394218, 40.0865833610807, 24.1288862068719, 62.8734959914056,
52.3161174228334, 32.6827415750796, 50.3593375610126), juliandate = c(323L,
346L, 318L, 314L, 324L, 325L, 311L, 342L, 341L, 348L, 328L, 345L,
328L, 328L, 326L, 312L, 340L, 331L, 326L, 320L, 359L, 323L, 312L,
337L, 347L, 351L, 352L, 320L, 326L, 345L, 352L, 342L, 312L, 323L,
333L, 348L, 340L, 330L, 338L, 323L, 356L, 345L, 359L, 334L, 320L,
349L, 358L, 355L, 321L, 324L, 6L, 52L, 46L, 21L, 47L, 1L, 43L,
14L, 48L, 2L, 60L, 21L, 27L, 26L, 37L, 51L, 2L, 1L, 43L, 57L,
23L, 49L, 10L, 38L, 58L, 20L, 3L, 45L, 1L, 17L, 30L, 42L, 5L,
58L, 1L, 4L, 7L, 38L, 56L, 53L, 14L, 46L, 54L, 36L, 52L, 6L,
32L, 35L, 46L, 3L)), .Names = c("nitro", "juliandate"), row.names = c(NA,
100L), class = "data.frame")
ggplot(d,aes(juliandate,nitro))+geom_point() #plot - should start at julian 305, end at julian 60
答案 0 :(得分:1)
我看到的最佳解决方案是将它们全部映射到一年并使用ggplot2
功能绘制该年份,以使日期看起来像朱利安日期。
因此解决方案有两个部分,一个映射和一个显示。在您的应用程序中,我会将它们视为数据框中的单独列。
下面介绍的解决方案使用包装函数(j2d
)进行包装日期(180天)的映射。然后,它使用ggplot2
函数scale_x_date
将输出日期转换为Julian格式:
library(ggplot2)
j2d <- function(n){
n <- ifelse( n<180, n+365, n ) # wrap at midyear
d <- as.Date("2015-01-01") + n
return(d)
}
df <-
structure(
list(
nitro =
c(36, 51, 23, 7, 26, 62, 45, 25, 31, 70,
23, 63, 41, 61, 6, 24, 2, 6, 47, 22, 6, 48, 16, 25, 61, 9, 63,
3, 22, 27, 52, 9, 34, 48, 31, 16, 51, 63, 21, 42, 4, 9, 20, 28,
23, 0, 23, 70, 47, 2, 52.0898926784594, 55.8022284640568, 41.3593089128863,
57.9928202206287, 35.4624211011211, 65.2484486428607, 39.236626265918,
40.8906848288849, 65.7044590718657, 39.9377208120021, 51.1368297792738,
46.3981830973577, 36.3795615108741, 59.4071137999101, 42.77053802297,
51.4293854584156, 45.3589706700315, 53.9652146584217, 54.6980728446628,
43.1278297603877, 48.2879709803009, 60.0627232974633, 49.5233404192228,
55.4231572422576, 35.9842318889366, 55.649768270307, 61.8309485366259,
63.1600858734154, 52.9116478602541, 48.5690637010561, 49.4065375720883,
44.7880773761964, 48.5893427534388, 43.5882970429564, 33.0365374509187,
40.2149387649389, 67.3129711280128, 55.491540487927, 63.5614232930341,
53.6421935293565, 47.0292298358771, 62.5727076637489, 32.2728901281264,
61.7219468394218, 40.0865833610807, 24.1288862068719, 62.8734959914056,
52.3161174228334, 32.6827415750796, 50.3593375610126),
juliandate =
c(323L,
346L, 318L, 314L, 324L, 325L, 311L, 342L, 341L, 348L, 328L, 345L,
328L, 328L, 326L, 312L, 340L, 331L, 326L, 320L, 359L, 323L, 312L,
337L, 347L, 351L, 352L, 320L, 326L, 345L, 352L, 342L, 312L, 323L,
333L, 348L, 340L, 330L, 338L, 323L, 356L, 345L, 359L, 334L, 320L,
349L, 358L, 355L, 321L, 324L, 6L, 52L, 46L, 21L, 47L, 1L, 43L,
14L, 48L, 2L, 60L, 21L, 27L, 26L, 37L, 51L, 2L, 1L, 43L, 57L,
23L, 49L, 10L, 38L, 58L, 20L, 3L, 45L, 1L, 17L, 30L, 42L, 5L,
58L, 1L, 4L, 7L, 38L, 56L, 53L, 14L, 46L, 54L, 36L, 52L, 6L,
32L, 35L, 46L, 3L)),
.Names = c("nitro", "juliandate"),
row.names = c(NA, 100L), class = "data.frame")
df$juliandate<-j2d(df$juliandate)
ggplot(df,aes(juliandate,nitro))+
geom_point() +
scale_x_date(labels = date_format("%j"))