I have a datset, AIS_dat, which looks at the number of boats (BoatCount) present at three sites (Site) on different days of the week (Day), before and during a Covid lockdown.
rm(list = ls())
setwd('K:/SoundTrap/Boats/PSD Output/Duty cycle data/TOL analysis')
getwd()
AIS_dat<-read.csv("AllSitesConcat_dBcalcs_50-24000Hz_matchedCameraCounts.csv")
str(AIS_dat)
#set factors
AIS_dat$Lockdown <- as.factor(AIS_dat$Lockdown)
#change order of sites
AIS_dat$Site<-factor(AIS_dat$Site,
level=c('Kawau','Tiritiri','Noises'))
#change order of days
AIS_dat$Day<-factor(AIS_dat$Day,
level=c('Mon','Tue','Wed','Thu','Fri','Sat','Sun'))
#hour of day as factor
AIS_dat$Hour <- as.factor(AIS_dat$Hour)
#Look at variation between sites
bp<-ggplot(AIS_dat, aes(x=Day,y=BoatCount,fill=factor(Site)))
geom_boxplot()
ylab(expression("Number of Boats"))
xlab("Day of Week")
scale_fill_manual(values = get_pal("Kereru"),
name="Site")
theme_bw()
bp
bp<-bp theme(axis.text.x = element_text(angle = 0,size=14),
axis.text.y = element_text(size=14),
axis.title.x = element_text(size=14),
axis.title.y =element_text(size=14),
#legend.title = element_text(size = 14),
#legend.text = element_text(size = 14)
) #rotate x-axis labels
bp<-bp facet_grid(rows=vars(Lockdown)) #separate plot for each season
bp
The plot looks like this
...which is awesome. BUT, because there is no data for Tiritiri under 'During', we only see two sites, and it is a bit misleading as the order of the sites then changes. I'd like to have missing data in this panel to make it clear which site is which. How would I do this? Would I have to add missing data to my original datasheet somehow? I tried this but it didn't work, so if that is the best approach I am not sure which way to do it?
The dataset looks like this (but has 17143 rows):
CodePudding user response:
You can combine site
and day
into a new column used for the x axis ticks. The function facet
will arrange the x tick values by default in a fixed way:
library(tidyverse)
set.seed(1337)
before_data <-
tribble(
~step, ~Site,
"before", "K",
"before", "T",
"before", "N"
) %>%
mutate(
value = rnorm(10) %>% list()
) %>%
unnest(value)
during_data <-
tribble(
~step, ~Site,
"during", "K",
"during", "N"
) %>%
mutate(
value = rnorm(10) %>% list()
) %>%
unnest(value)
data <- bind_rows(before_data, during_data) %>% mutate(day = "Monday")
data %>%
mutate(x = paste0(day, Site)) %>%
ggplot(aes(x, value, color = Site))
geom_boxplot()
facet_grid(~step)
Created on 2021-09-13 by the reprex package (v2.0.1)