0

I am trying to produce a ggplot which shows the period of time that birds spent flying over a site on each day in response to one of four treatments, grouped by Block. There are 6 blocks. Each treatment was administered on a different day over a two-hour time period.

The first issue is that when using facet_grid, 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.

Secondly, despite reading various tutorials, I have been unable to work out how to deal with the time duration (variable = Total.Time) i.e. the minutes and seconds the birds were present. I'd like to use the Total.Time_mins variable to show the total time (minutes and seconds) spent prospecting over the site. The scale is clearly off as the difference between 15 minutes and 2 seconds should be notably larger. The time data in the spreadsheet was entered as HH:MM:SS.

I have attached a copy of the graph, which shows the respective issues:

plot

A sample of the data is provided below in created variables labelled Date, Total.Time_mins and Treatment

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)

Here is the code I have produced thus far.

[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
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Greeny
  • 7
  • 6
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 12 '19 at 03:03
  • Thank you, Tung. I have edited my problem above and provided a sample of the data for you to see. – Greeny Nov 12 '19 at 06:28
  • I cannot reproduce the plot. I've assumed `Data` is a data.frame of `Date`, `Total.Time_mins` and `Treatment`, but it is still missing a `Block` variable in order for the code to work. – teunbrand Nov 12 '19 at 08:12
  • 1
    My apologies. I have edited the code and it is now runs using the manually-created data frame. – Greeny Nov 12 '19 at 10:01

1 Answers1

0

ggplot has scale_y_time which work with hms class objects from the hms package. If you convert to that class, it will be used automatically.

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)

enter image description here

I also

  • removed all the theme options as they seemed unrelated to the question
  • switched to geom_col instead of geom_bar(stat = "identity")
  • don't see what you mean about "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." (Maybe you don't want the full scale on each x-axis? You could replace your facet_grid with facet_wrap(~ Blocks, scales = "free_x")... but since your sample data has 4 days per block, and you explicitly ask for breaks on the axis every 4 days, the bars will be clustered around a single axis break/label)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 1
    Thank you, Gregor. As per your comments, I have adjusted the code slightly so the data for each block is not clustered around the first date within each block, i.e. 'facet_grid(. ~ Blocks, scales = "free_x")'. I appreciate your assistance :) – Greeny Nov 13 '19 at 07:06