Aim create bar chart where xaxis=day, yaxis=mass and fill=group
I have used pivot wider to get the data into a usable format. However I am stuck on how to summarise the data for inputing it into the bar chart - when i try and use summarise i get one summary statistic despite grouping by day and group
study_id <- c(1,2,3,4,5,6)
group <- c(1,2,1,2,1,2)
mass_day1 <-c(NA,2,NA,3,NA,1)
mass_day2 <- c(15,15,15,15,NA,15)
mass_day3 <-c(3,3,3,3,3,3)
mass_day4 <- c(4,4,4,NA,4,4)
drink_df <- data.frame(study_id,group,mass_day4,mass_day3,mass_day2,mass_day1)
drink_dfa <- drink_df %>%
pivot_longer(cols = starts_with("mass"),
names_to = c(".value", "day"),
names_pattern = '(.*)_(.*)',
values_drop_na = TRUE) %>%
dplyr::group_by(day,group) %>% summarise(
mass = mean(mass), na.rm = TRUE) %>% ungroup()
plot <- ggplot(drink_dfa, aes(x = day, y = mass, fill = group)) +
geom_bar(stat="identity", position=position_dodge(), na.rm = FALSE)