2

I'm trying to create a grouped bar plot similar to the one in the image below.

This is the bar plot that I am trying to emulate

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!

1 Answers1

0

Here's exactly what I've done. Your problem is not reproducible. You may have some changes in your RStudio settings.

library(ggplot2)
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"))

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")

P.S. This is equivalent to first assigning the object and then print the object with calling it directly or with print

Created on 2020-04-18 by the reprex package (v0.3.0)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • hmm okay, this is really weird/frustrating. I checked out my settings and made sure that my output preview is in the viewer pane. I've quit and restarted my R session a few times now. If you have specific recommendations for other settings to modify that would be great. Based on the other sources I've read through it seems like there isn't much else...```dev.off()``` doesn't seem to be a solution, and I've replaced show with print(Plot1) in my code... – Salma Abdel-Raheem Apr 18 '20 at 10:48
  • @SalmaAbdel-Raheem maybe create a completely new project. And/or restart RStudio and/or reinstall it new. ? – tjebo Apr 18 '20 at 12:41