【问题标题】:R ggplot2 faceting standardizing date limitsR ggplot2 刻面标准化日期限制
【发布时间】:2017-12-09 17:58:22
【问题描述】:

我正在尝试创建一个包含 3 个调查季节的图表,每个调查季节都跨越一年的边界(即 2009-2010、2010-2011、2016-2017),但调查工作在每次调查的不同点开始和结束季节。我目前拥有的是:

我希望每个 x 轴显示该调查季的 11 月至 4 月(即 2009-2010 年调查季将显示 2009 年 11 月至 2010 年 4 月),以更好地显示调查工作在哪些方面缺乏年际比较。我只是不确定如何在scale_x_date() 的上下文中做到这一点。我目前的比例声明只是scale_x_date(date_breaks = '1 month',date_labels='%b')+ 到目前为止,我唯一的想法是基本上只创建一堆具有相关日期的 NA 数据并继续让日期自动缩放,但为了我未来的参考,我想知道是否有更好的创建这种“无年”日期限制的方法。

更多信息:

我的数据的头部(在这种情况下将在图的左上角):

structure(list(SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001", 
"Cho_001", "Cho_001"), season = c(2009, 2009, 2009, 2009, 2009, 
2009), Date = structure(c(14579, 14580, 14581, 14582, 14583, 
14584), class = "Date"), DayOfYear = c(335, 336, 337, 338, 339, 
340), year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L), month = c(12L, 
12L, 12L, 12L, 12L, 12L), day = 1:6, RatePerMin = c(3.6667, 4.8667, 
13.1667, 23.0333, 24.6667, 25.4667), N = c(3L, 30L, 30L, 30L, 
30L, 30L), sd = c(3.5119, 6.9269, 8.1074, 4.2789, 4.9434, 4.2729
), se = c(2.0276, 1.2647, 1.4802, 0.7812, 0.9025, 0.7801), Illu = c(0.999348417128253, 
0.999986511310919, 0.991435775301254, 0.955915838251852, 0.894428866388813, 
0.810868946410883)), .Names = c("SPID", "season", "Date", "DayOfYear", 
"year", "month", "day", "RatePerMin", "N", "sd", "se", "Illu"
), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = c("SPID", "season", "year", "month", 
"day", "Date"), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L, 
    5L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001", 
    "Cho_001"), season = c(2009, 2009, 2009, 2009, 2009, 2009
    ), year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L), month = c(12L, 
    12L, 12L, 12L, 12L, 12L), day = 1:6, Date = structure(c(14579, 
    14580, 14581, 14582, 14583, 14584), class = "Date")), row.names = c(NA, 
-6L), class = "data.frame", vars = c("SPID", "season", "year", 
"month", "day", "Date"), drop = TRUE, .Names = c("SPID", "season", 
"year", "month", "day", "Date")))

【问题讨论】:

  • 您能否在问题中添加一个(小)数据集示例?
  • 你在 facet_grid 中试过 scales = "free" 吗?
  • ed_sans:是的,我有。结果是链接的数字,并根据数据确定限制。
  • aosmith:数据已经在上面了。

标签: r date ggplot2


【解决方案1】:

根据 ed_sans 建议的一些内容,想出了这一点。最终创建了一个新列:

Rate_by_site_night$DaysSinceNov1<-as.numeric(Rate_by_site_night$Date-
as.Date(paste0(Rate_by_site_night$season, '-11-01')))

并将其用作带有scale_x_continuous 参数的x 轴变量,并将这些变量用作中断和标签:

test_breaks=c(0,30,61,92,120,151) #month boundaries in terms of days since Nov 1
test_labels=format(as.Date(test_breaks, origin='2016-11-01'), format= '%b')

幸运的是,我的年份都不是闰年,在这种情况下,我在 3 月和 4 月的月份边界中断将会关闭。

【讨论】:

    【解决方案2】:

    我们可以使用 Nov-01 之后的一天在 x 轴上绘制,这取决于月份和日期。

    data$start <- ymd(paste(year(data$Date), "-11-01", sep=""))
    data$yday = data$Date - data$start #yday(data$Date)
    data$yday = ifelse(data$yday < 0, 365 + data$yday,data$yday)
    
    bre <- seq(1,152,1)
    lab <- format(seq.Date(ymd("2010-11-01"),ymd("2011-04-01"),by="day"),format= "%b %d")
    
    library(ggplot2)
    library(lubridate)
    x11(); ggplot(data) + geom_bar(aes(x = yday , y = RatePerMin), stat = "identity") +
      scale_x_continuous(breaks = bre,
      labels = lab, limits = c(1,152))+ theme(axis.text.x = element_text(angle = 90, hjust = 1))+
      facet_grid(year ~ .)  
    

    PS:我认为添加一些 NA 值(在每个 Nov-01 和 Apr 01)会比这个解决方案更有效。

    数据:

    structure(list(SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001", 
    "Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001", 
    "Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001"), season = c(2009, 
    2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 
    2009, 2009, 2009), Date = structure(c(14579, 14580, 14581, 14582, 
    14583, 14584, 14944, 14945, 14946, 14947, 14948, 14949, 14610, 
    14611, 14612), class = "Date"), DayOfYear = c(335, 336, 337, 
    338, 339, 340, 335, 336, 337, 338, 339, 340, 335, 336, 337), 
        year = c(2009, 2009, 2009, 2009, 2009, 2009, 2010, 2010, 
        2010, 2010, 2010, 2010, 2010, 2010, 2010), month = c(12L, 
        12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
        12L, 12L), day = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
        5L, 6L, 1L, 2L, 3L), RatePerMin = c(3.6667, 4.8667, 13.1667, 
        23.0333, 24.6667, 25.4667, 3.6667, 4.8667, 13.1667, 23.0333, 
        24.6667, 25.4667, 3.6667, 4.8667, 13.1667), N = c(3L, 30L, 
        30L, 30L, 30L, 30L, 3L, 30L, 30L, 30L, 30L, 30L, 3L, 30L, 
        30L), sd = c(3.5119, 6.9269, 8.1074, 4.2789, 4.9434, 4.2729, 
        3.5119, 6.9269, 8.1074, 4.2789, 4.9434, 4.2729, 3.5119, 6.9269, 
        8.1074), se = c(2.0276, 1.2647, 1.4802, 0.7812, 0.9025, 0.7801, 
        2.0276, 1.2647, 1.4802, 0.7812, 0.9025, 0.7801, 2.0276, 1.2647, 
        1.4802), Illu = c(0.999348417128253, 0.999986511310919, 0.991435775301254, 
        0.955915838251852, 0.894428866388813, 0.810868946410883, 
        0.999348417128253, 0.999986511310919, 0.991435775301254, 
        0.955915838251852, 0.894428866388813, 0.810868946410883, 
        0.999348417128253, 0.999986511310919, 0.991435775301254), 
        yday = c(30, 31, 32, 33, 34, 35, 30, 31, 32, 33, 34, 35, 
        61, 62, 63), start = structure(c(14549, 14549, 14549, 14549, 
        14549, 14549, 14914, 14914, 14914, 14914, 14914, 14914, 14914, 
        14914, 14914), class = "Date"), end = structure(c(14700, 
        14700, 14700, 14700, 14700, 14700, 15065, 15065, 15065, 15065, 
        15065, 15065, 15065, 15065, 15065), class = "Date")), .Names = c("SPID", 
    "season", "Date", "DayOfYear", "year", "month", "day", "RatePerMin", 
    "N", "sd", "se", "Illu", "yday", "start", "end"), row.names = c(NA, 
    15L), vars = c("SPID", "season", "year", "month", "day", "Date"
    ), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L, 5L), group_sizes = c(1L, 
    1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
        SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001", 
        "Cho_001"), season = c(2009, 2009, 2009, 2009, 2009, 2009
        ), year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L), month = c(12L, 
        12L, 12L, 12L, 12L, 12L), day = 1:6, Date = structure(c(14579, 
        14580, 14581, 14582, 14583, 14584), class = "Date")), .Names = c("SPID", 
    "season", "year", "month", "day", "Date"), row.names = c(NA, 
    -6L), class = "data.frame", vars = c("SPID", "season", "year", 
    "month", "day", "Date"), drop = TRUE), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"))
    

    【讨论】:

    • 我不确定在这里使用基于日期的 x 轴是一个可行的解决方案,因为数据围绕着一年的变化(例如,一个季节可能从 2009 年 12 月开始,并且2010 年 3 月结束)。为了更清楚地了解我想要做什么,我试图确保每个多年的方面都在方面的第一年的 11 月 1 日开始,并在方面的第二年的 4 月 1 日结束,不管是哪一年。
    • 您可以根据可能与 Jan-01 不同的来源使用一年中的某一天,例如 Dec-01
    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多