按因素和持续时间划分的方面网格

Facet grid by factor and time duration

我正在尝试制作一个 ggplot,它显示鸟类每天飞越某个地点以响应四种处理方式之一的时间段,按 Block 分组。有6块。每次治疗都在不同的一天进行,持续两小时。

第一个问题是,当使用 facet_grid 时,每个块的所有数据点都在每个块的第一个日期分组,而不是分布在块内的四天。

其次,尽管阅读了各种教程,但我一直无法弄清楚如何处理持续时间(变量 = Total.Time),即鸟类出现的分钟和秒数。我想使用 Total.Time_mins 变量来显示在网站上进行勘探所花费的总时间(分钟和秒)。刻度明显偏离,因为 15 分钟和 2 秒之间的差异应该明显更大。电子表格中的时间数据输入为 HH:MM:SS.

我附上了图表的副本,其中显示了各个问题:

下面在标记为日期、Total.Time_mins 和治疗

的已创建变量中提供了数据示例
Dates <- as.Date(c("0019-10-07", "0019-10-08", "0019-10-09", "0019-10-10", "0019-10-11", "0019-10-12", "0019-10-13", "0019-10-14", "0019-10-14", "0019-10-15","0019-10-16", "0019-10-17"))
Total.Time <- c("0:02:27", "0:00:16","0:00:22", "0:00:03", "0:00:00", "0:00:20", "0:01:32", "0:09:11", "0:00:30", "0:07:26", "0:00:59", "0:14:13")
Treat <- c("Playback", "Control", "Decoys & Playback", "Decoys", "Control", "Decoys", "Playback", "Decoys & Playback", "Decoys", "Playback", "Control", "Decoys & Playback")
Blocks <- c("1","1","1","1","2","2","2","2","3","3","3","3")
Data1 <- data.frame(Dates, Total.Time, Treat, Blocks)

这是我到目前为止生成的代码。

[names(Data)
 \[1\] "Dates"                 "Blocks"                "Treat"            "Observation"          "Total.Time"     
 \[6\] "Time"                 "Max_Birds"            "Landing"              "Wind_Speed"           "Beaufort_Force_Scale"
\[11\] "Wind_Direction" 

# Convert the date information into R's date format.  R will automatically treat these dates as numeric in our analyses.
Data$Date <- as.Date(Data$Date, format = "%d/%m/%Y")

# Create GGPlot of time spent prospecting over site
p = ggplot(Data1, aes(Dates, Total.Time, fill=Treat)) + geom_bar(stat = "identity") 
p = p + facet_grid(.~ Blocks)
p = p + scale_fill_grey(start=0.8, end=0.0)
p = p + theme(panel.background = element_blank())
p = p + theme(axis.text.x=element_text(size=16, vjust=0.6),  
              axis.title.x=element_text(face="bold", size=16, vjust=-6),
              axis.title.y=element_text(face="bold", size=16),
              axis.text.y=element_text(size=16), 
              legend.text = element_text(size=12), 
              axis.line=element_line())
p = p + theme(axis.text.x=element_text(size=16, vjust=0.6)) 
#p = p + scale_x_date(date_labels="%d %b", date_breaks = "4 day", breaks = 4)
p = p + theme(axis.text.x = element_text(face="bold", size=10, angle=90))
p

ggplot 具有 scale_y_time,它与 hms 包中的 hms class 对象一起使用。如果您转换为 class,它将自动使用。

Data1$total_time_hms = hms::as_hms(as.character(Data1$Total.Time))

ggplot(Data1, aes(Dates, total_time_hms, fill=Treat)) +
  geom_col()  +
  facet_grid(. ~ Blocks) +
  scale_fill_grey(start=0.8, end=0.0) +
  scale_x_date(date_labels="%d %b", date_breaks = "4 day", breaks = 4)

我也

  • 删除了所有 theme 选项,因为它们似乎与问题无关
  • 切换到 geom_col 而不是 geom_bar(stat = "identity")
  • 不明白你对 "all data points for each Block are grouped on the first date of each Block, rather than being spread across the four days within the Block." 的意思(也许你不想要每个 x 轴上的满刻度?你可以替换你的 facet_gridfacet_wrap(~ Blocks, scales = "free_x")... 但由于您的示例数据每个块有 4 天,并且您明确要求每 4 天在轴上休息一次,条形图将围绕单个轴聚集 break/label )