I'm trying to create a grouped bar plot similar to the one in the image below.
I've tried to adapt the code below for my plot for my "SRKWyear_final" df, but instead of generating a plot in the "plot" or "viewer" window I instead see my plot in the environments as a list of 9 items. I'm not sure why this is happening as I am not getting any sort of error, but I'm also not getting any sort of output. The three fill types are from the three unique values of the the "quadID" column. For the fill colors, I had wanted "khaki1" to go with "Transit" values from quadID, "darkorange1" to go with "Adjacent" values, and "red3" to go with "ZOI" values. Here is the code that I tried to use:
Plot1 <- ggplot(SRKWyear_final, aes(x=as.numeric(Year), y=whale_days, fill=quadID)) +
geom_col(position = position_dodge2(width = 0.9, preserve = "single")) +
theme_bw() +
theme(axis.title = element_blank(),
legend.title = element_blank(),
panel.grid = element_blank(),
legend.text = element_text(margin = margin(l = 3), hjust = 0),
plot.title = element_text(hjust = 0.5, size = 12)) +
scale_fill_manual(values = c("khaki1",
"darkorange1",
"red3")) +
scale_x_discrete(drop = FALSE) +
ggtitle("Whale Days by Year")
show(Plot1)
Here is my data frame:
> head(SRKWyear_final)
# A tibble: 6 x 3
# Groups: Year [4]
Year quadID whale_days
<dbl> <chr> <dbl>
1 2009 Adjacent 1
2 2009 Transit 165
> dput(droplevels(SRKWyear_final))
structure(list(Year = c(2009, 2009, 2010, 2010, 2011, 2012, 2012,
2013, 2014, 2014, 2015, 2015, 2016, 2017, 2018, 2009, 2010, 2011,
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 2013, 2016, 2017,
2018), quadID = c("Adjacent", "Transit", "Adjacent", "Transit",
"Transit", "Adjacent", "Transit", "Transit", "Adjacent", "Transit",
"Adjacent", "Transit", "Transit", "Transit", "Transit", "ZOI",
"ZOI", "ZOI", "ZOI", "ZOI", "ZOI", "ZOI", "ZOI", "ZOI", "ZOI",
"Adjacent", "Adjacent", "Adjacent", "Adjacent", "Adjacent"),
whale_days = c(1, 165, 1, 114, 157, 1, 226, 152, 6, 240,
4, 158, 224, 248, 227, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0)), row.names = c(NA, -30L), groups = structure(list(
Year = c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018), .rows = list(c(1L, 2L, 16L), c(3L, 4L, 17L),
c(5L, 18L, 26L), c(6L, 7L, 19L), c(8L, 20L, 27L), c(9L,
10L, 21L), c(11L, 12L, 22L), c(13L, 23L, 28L), c(14L,
24L, 29L), c(15L, 25L, 30L))), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Any insight would be much appreciated!