ggplot geom_bar showing blank graph

Hi,

I ran the following code but got a blank plot. I am quite new to R. Can anyone point me to a possible error in my code? Thanks in advance.

p_medi <- ggplot(ASD_State_MEDI, aes(x = 1, fill = State_Full2)) +
  geom_bar() + theme(axis.text.x=element_blank(),  # Hide axis
                     axis.ticks.x=element_blank(), # Hide axis
                     axis.text.y=element_blank(),  # Hide axis
                     axis.ticks.y=element_blank(), # Hide axis
                     panel.background = element_blank(), # Remove panel background
                     legend.position="none",
                     strip.text.y = element_text(angle=0) # Rotate text to horizontal
  ) +
  facet_grid(facets = State_Full2 ~ Year_Factor) +
  labs(x="", y="", title="Years Data Available by State [ Source: MEDI ]") # layers of graphics

p_medi

I ran your code on my own data and got a non-blank plot.
So perhaps the issue is your data.
Can you run the following in the console and paste over the result ?
(it gives us a sample of your data)

ASD_State_MEDI %>% 
select(State_Full2,Year_Factor) %>% 
slice(sample.int(n=nrow(ASD_State_MEDI), 
                 size=50, replace = FALSE)) %>% 
dput()

Hi, thanks for the super quick reply. This is what I am getting after running your code.


structure(list(State_Full2 = structure(c(12L, 44L, 51L, 46L, 
32L, 18L, 7L, 17L, 36L, 22L, 20L, 33L, 50L, 45L, 31L, 14L, 28L, 
50L, 39L, 15L, 11L, 48L, 15L, 36L, 48L, 14L, 35L, 1L, 19L, 1L, 
33L, 13L, 50L, 28L, 23L, 32L, 35L, 8L, 25L, 51L, 31L, 43L, 16L, 
28L, 3L, 16L, 41L, 20L, 25L, 13L), .Label = c("AK-Alaska", "AL-Alabama", 
"AR-Arkansas", "AZ-Arizona", "CA-California", "CO-Colorado", 
"CT-Connecticut", "DC-District of Columbia", "DE-Delaware", "FL-Florida", 
"GA-Georgia", "HI-Hawaii", "IA-Iowa", "ID-Idaho", "IL-Illinois", 
"IN-Indiana", "KS-Kansas", "KY-Kentucky", "LA-Louisiana", "MA-Massachusetts", 
"MD-Maryland", "ME-Maine", "MI-Michigan", "MN-Minnesota", "MO-Missouri", 
"MS-Mississippi", "MT-Montana", "NC-North Carolina", "ND-North Dakota", 
"NE-Nebraska", "NH-New Hampshire", "NJ-New Jersey", "NM-New Mexico", 
"NV-Nevada", "NY-New York", "OH-Ohio", "OK-Oklahoma", "OR-Oregon", 
"PA-Pennsylvania", "RI-Rhode Island", "SC-South Carolina", "SD-South Dakota", 
"TN-Tennessee", "TX-Texas", "UT-Utah", "VA-Virginia", "VT-Vermont", 
"WA-Washington", "WI-Wisconsin", "WV-West Virginia", "WY-Wyoming"
), class = "factor"), Year_Factor = structure(c(1L, 1L, 1L, 13L, 
13L, 12L, 5L, 8L, 1L, 5L, 11L, 8L, 13L, 9L, 9L, 5L, 4L, 3L, 10L, 
2L, 13L, 1L, 7L, 5L, 10L, 13L, 1L, 1L, 4L, 11L, 13L, 8L, 1L, 
6L, 1L, 8L, 7L, 4L, 11L, 12L, 11L, 10L, 8L, 11L, 6L, 2L, 8L, 
13L, 6L, 1L), .Label = c("2000", "2001", "2002", "2003", "2004", 
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", 
"2013", "2014", "2015", "2016"), class = c("ordered", "factor"
))), row.names = c(NA, -50L), class = "data.frame")

The data and code you provided does generate a graph.

#full reprex
library(tidyverse)
ASD_State_MEDI<-structure(list(State_Full2 = structure(c(12L, 44L, 51L, 46L, 
                                         32L, 18L, 7L, 17L, 36L, 22L, 20L, 33L, 50L, 45L, 31L, 14L, 28L, 
                                         50L, 39L, 15L, 11L, 48L, 15L, 36L, 48L, 14L, 35L, 1L, 19L, 1L, 
                                         33L, 13L, 50L, 28L, 23L, 32L, 35L, 8L, 25L, 51L, 31L, 43L, 16L, 
                                         28L, 3L, 16L, 41L, 20L, 25L, 13L), .Label = c("AK-Alaska", "AL-Alabama", 
                                                                                       "AR-Arkansas", "AZ-Arizona", "CA-California", "CO-Colorado", 
                                                                                       "CT-Connecticut", "DC-District of Columbia", "DE-Delaware", "FL-Florida", 
                                                                                       "GA-Georgia", "HI-Hawaii", "IA-Iowa", "ID-Idaho", "IL-Illinois", 
                                                                                       "IN-Indiana", "KS-Kansas", "KY-Kentucky", "LA-Louisiana", "MA-Massachusetts", 
                                                                                       "MD-Maryland", "ME-Maine", "MI-Michigan", "MN-Minnesota", "MO-Missouri", 
                                                                                       "MS-Mississippi", "MT-Montana", "NC-North Carolina", "ND-North Dakota", 
                                                                                       "NE-Nebraska", "NH-New Hampshire", "NJ-New Jersey", "NM-New Mexico", 
                                                                                       "NV-Nevada", "NY-New York", "OH-Ohio", "OK-Oklahoma", "OR-Oregon", 
                                                                                       "PA-Pennsylvania", "RI-Rhode Island", "SC-South Carolina", "SD-South Dakota", 
                                                                                       "TN-Tennessee", "TX-Texas", "UT-Utah", "VA-Virginia", "VT-Vermont", 
                                                                                       "WA-Washington", "WI-Wisconsin", "WV-West Virginia", "WY-Wyoming"
                                         ), class = "factor"), Year_Factor = structure(c(1L, 1L, 1L, 13L, 
                                                                                         13L, 12L, 5L, 8L, 1L, 5L, 11L, 8L, 13L, 9L, 9L, 5L, 4L, 3L, 10L, 
                                                                                         2L, 13L, 1L, 7L, 5L, 10L, 13L, 1L, 1L, 4L, 11L, 13L, 8L, 1L, 
                                                                                         6L, 1L, 8L, 7L, 4L, 11L, 12L, 11L, 10L, 8L, 11L, 6L, 2L, 8L, 
                                                                                         13L, 6L, 1L), .Label = c("2000", "2001", "2002", "2003", "2004", 
                                                                                                                  "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", 
                                                                                                                  "2013", "2014", "2015", "2016"), class = c("ordered", "factor"
                                                                                                                  ))), row.names = c(NA, -50L), class = "data.frame")

 
p_medi <- ggplot(ASD_State_MEDI, aes(x = 1, fill = State_Full2)) +
  geom_bar() + theme(axis.text.x=element_blank(),  # Hide axis
                     axis.ticks.x=element_blank(), # Hide axis
                     axis.text.y=element_blank(),  # Hide axis
                     axis.ticks.y=element_blank(), # Hide axis
                     panel.background = element_blank(), # Remove panel background
                     legend.position="none",
                     strip.text.y = element_text(angle=0) # Rotate text to horizontal
  ) +
  facet_grid(facets = State_Full2 ~ Year_Factor) +
  labs(x="", y="", title="Years Data Available by State [ Source: MEDI ]") # layers of graphics

p_medi

Therefore perhaps there is an issue with your session.
I recommend using Ctrl+Shift+F10 to restart R.

I did that, but I am still getting a blank plot. The odd thing is, there are 2 chunks of similar codes before this that each plots a different subset of the same data frame. One of these also plots a blank graph, but the other one shows up a graph fine. I am attaching screenshots of these two chunks and the graphs they plot. Any idea what is causing this? Thanks!


If you want to share the blank/first plots subset info then do the dput again but change the variable names for the ones that are used in the plotting code.

Hi, here is the dput output. Thanks.

> ASD_State %>% 
+   select(State_Full2,Year_Factor) %>% 
+   slice(sample.int(n=nrow(ASD_State_MEDI), 
+                    size=50, replace = FALSE)) %>% 
+   dput()
structure(list(State_Full2 = structure(c(40L, 21L, 35L, 27L, 
46L, 49L, 12L, 21L, 26L, 46L, 47L, 30L, 39L, 3L, 11L, 21L, 28L, 
19L, 21L, 25L, 15L, 6L, 47L, 9L, 23L, 29L, 16L, 14L, 19L, 7L, 
20L, 6L, 4L, 32L, 39L, 25L, 1L, 16L, 3L, 50L, 30L, 41L, 31L, 
2L, 40L, 29L, 31L, 27L, 9L, 40L), .Label = c("AK-Alaska", "AL-Alabama", 
"AR-Arkansas", "AZ-Arizona", "CA-California", "CO-Colorado", 
"CT-Connecticut", "DC-District of Columbia", "DE-Delaware", "FL-Florida", 
"GA-Georgia", "HI-Hawaii", "IA-Iowa", "ID-Idaho", "IL-Illinois", 
"IN-Indiana", "KS-Kansas", "KY-Kentucky", "LA-Louisiana", "MA-Massachusetts", 
"MD-Maryland", "ME-Maine", "MI-Michigan", "MN-Minnesota", "MO-Missouri", 
"MS-Mississippi", "MT-Montana", "NC-North Carolina", "ND-North Dakota", 
"NE-Nebraska", "NH-New Hampshire", "NJ-New Jersey", "NM-New Mexico", 
"NV-Nevada", "NY-New York", "OH-Ohio", "OK-Oklahoma", "OR-Oregon", 
"PA-Pennsylvania", "RI-Rhode Island", "SC-South Carolina", "SD-South Dakota", 
"TN-Tennessee", "TX-Texas", "UT-Utah", "VA-Virginia", "VT-Vermont", 
"WA-Washington", "WI-Wisconsin", "WV-West Virginia", "WY-Wyoming"
), class = "factor"), Year_Factor = structure(c(9L, 15L, 4L, 
4L, 5L, 9L, 7L, 3L, 3L, 9L, 9L, 6L, 8L, 10L, 3L, 6L, 9L, 5L, 
5L, 2L, 9L, 5L, 5L, 4L, 17L, 2L, 13L, 8L, 7L, 5L, 6L, 6L, 5L, 
7L, 3L, 4L, 6L, 5L, 1L, 17L, 3L, 5L, 17L, 5L, 4L, 6L, 1L, 17L, 
8L, 9L), .Label = c("2000", "2001", "2002", "2003", "2004", "2005", 
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
"2014", "2015", "2016"), class = c("ordered", "factor"))), row.names = c(NA, 
-50L), class = "data.frame")

I think you omitted Source ?

Sorry, perhaps let me use the code below. Only p_addm gives a proper plot; p_medi gives an empty plot.

p_medi <- ggplot(ASD_State_MEDI, aes(x = 1, fill = State_Full2)) +
  geom_bar() + theme(axis.text.x=element_blank(),  # Hide axis
                     axis.ticks.x=element_blank(), # Hide axis
                     axis.text.y=element_blank(),  # Hide axis
                     axis.ticks.y=element_blank(), # Hide axis
                     panel.background = element_blank(), # Remove panel background
                     legend.position="none",
                     strip.text.y = element_text(angle=0) # Rotate text to horizontal
  ) +
  facet_grid(facets = State_Full2 ~ Year_Factor) +
  labs(x="", y="", title="Years Data Available by State [ Source: MEDI ]") # layers of graphics

p_medi

p_addm <- ggplot(ASD_State_ADDM, aes(x = 1, fill = State_Full2)) +
  geom_bar() + theme(axis.text.x=element_blank(),  # Hide axis
                     axis.ticks.x=element_blank(), # Hide axis
                     axis.text.y=element_blank(),  # Hide axis
                     axis.ticks.y=element_blank(), # Hide axis
                     panel.background = element_blank(), # Remove panel background
                     legend.position="none",
                     strip.text.y = element_text(angle=0) # Rotate text to horizontal
  ) +
  facet_grid(facets = State_Full2 ~ Year_Factor) +
  labs(x="", y="", title="Years Data Available by State [ Source: ADDM ]") # layers of graphics

p_addm

The dput for both MEDI and ADDM are:

> ASD_State_MEDI %>% 
+   select(State_Full2,Year_Factor) %>% 
+   slice(sample.int(n=nrow(ASD_State_MEDI), 
+                    size=50, replace = FALSE)) %>% 
+   dput()
structure(list(State_Full2 = structure(c(34L, 12L, 49L, 49L, 
48L, 50L, 11L, 9L, 15L, 2L, 46L, 15L, 17L, 12L, 3L, 47L, 9L, 
47L, 2L, 45L, 29L, 29L, 15L, 20L, 33L, 32L, 50L, 25L, 27L, 15L, 
7L, 9L, 3L, 8L, 28L, 30L, 6L, 9L, 37L, 41L, 3L, 50L, 40L, 2L, 
19L, 38L, 18L, 50L, 43L, 26L), .Label = c("AK-Alaska", "AL-Alabama", 
"AR-Arkansas", "AZ-Arizona", "CA-California", "CO-Colorado", 
"CT-Connecticut", "DC-District of Columbia", "DE-Delaware", "FL-Florida", 
"GA-Georgia", "HI-Hawaii", "IA-Iowa", "ID-Idaho", "IL-Illinois", 
"IN-Indiana", "KS-Kansas", "KY-Kentucky", "LA-Louisiana", "MA-Massachusetts", 
"MD-Maryland", "ME-Maine", "MI-Michigan", "MN-Minnesota", "MO-Missouri", 
"MS-Mississippi", "MT-Montana", "NC-North Carolina", "ND-North Dakota", 
"NE-Nebraska", "NH-New Hampshire", "NJ-New Jersey", "NM-New Mexico", 
"NV-Nevada", "NY-New York", "OH-Ohio", "OK-Oklahoma", "OR-Oregon", 
"PA-Pennsylvania", "RI-Rhode Island", "SC-South Carolina", "SD-South Dakota", 
"TN-Tennessee", "TX-Texas", "UT-Utah", "VA-Virginia", "VT-Vermont", 
"WA-Washington", "WI-Wisconsin", "WV-West Virginia", "WY-Wyoming"
), class = "factor"), Year_Factor = structure(c(10L, 7L, 12L, 
11L, 12L, 7L, 11L, 2L, 2L, 7L, 1L, 11L, 3L, 4L, 8L, 12L, 6L, 
3L, 5L, 10L, 3L, 8L, 13L, 1L, 1L, 1L, 12L, 13L, 9L, 7L, 2L, 13L, 
4L, 9L, 6L, 5L, 7L, 7L, 3L, 13L, 6L, 5L, 10L, 3L, 4L, 12L, 6L, 
6L, 7L, 12L), .Label = c("2000", "2001", "2002", "2003", "2004", 
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", 
"2013", "2014", "2015", "2016"), class = c("ordered", "factor"
))), row.names = c(NA, -50L), class = "data.frame")
> ASD_State_ADDM %>% 
+   select(State_Full2,Year_Factor) %>% 
+   slice(sample.int(n=nrow(ASD_State_ADDM), 
+                    size=50, replace = FALSE)) %>% 
+   dput()
structure(list(State_Full2 = structure(c(28L, 11L, 25L, 39L, 
3L, 21L, 11L, 21L, 49L, 2L, 3L, 49L, 32L, 32L, 25L, 45L, 4L, 
25L, 3L, 4L, 50L, 21L, 28L, 25L, 2L, 2L, 4L, 21L, 50L, 28L, 45L, 
11L, 45L, 2L, 25L, 28L, 41L, 45L, 24L, 10L, 28L, 4L, 32L, 2L, 
21L, 6L, 21L, 25L, 4L, 28L), .Label = c("AK-Alaska", "AL-Alabama", 
"AR-Arkansas", "AZ-Arizona", "CA-California", "CO-Colorado", 
"CT-Connecticut", "DC-District of Columbia", "DE-Delaware", "FL-Florida", 
"GA-Georgia", "HI-Hawaii", "IA-Iowa", "ID-Idaho", "IL-Illinois", 
"IN-Indiana", "KS-Kansas", "KY-Kentucky", "LA-Louisiana", "MA-Massachusetts", 
"MD-Maryland", "ME-Maine", "MI-Michigan", "MN-Minnesota", "MO-Missouri", 
"MS-Mississippi", "MT-Montana", "NC-North Carolina", "ND-North Dakota", 
"NE-Nebraska", "NH-New Hampshire", "NJ-New Jersey", "NM-New Mexico", 
"NV-Nevada", "NY-New York", "OH-Ohio", "OK-Oklahoma", "OR-Oregon", 
"PA-Pennsylvania", "RI-Rhode Island", "SC-South Carolina", "SD-South Dakota", 
"TN-Tennessee", "TX-Texas", "UT-Utah", "VA-Virginia", "VT-Vermont", 
"WA-Washington", "WI-Wisconsin", "WV-West Virginia", "WY-Wyoming"
), class = "factor"), Year_Factor = structure(c(13L, 13L, 15L, 
9L, 3L, 11L, 15L, 9L, 9L, 7L, 13L, 7L, 9L, 3L, 5L, 13L, 11L, 
11L, 9L, 13L, 3L, 15L, 5L, 13L, 3L, 9L, 5L, 3L, 1L, 3L, 3L, 3L, 
11L, 11L, 9L, 15L, 9L, 9L, 15L, 9L, 7L, 3L, 15L, 5L, 7L, 15L, 
1L, 7L, 15L, 11L), .Label = c("2000", "2001", "2002", "2003", 
"2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", 
"2012", "2013", "2014", "2015", "2016"), class = c("ordered", 
"factor"))), row.names = c(NA, -50L), class = "data.frame")

your code and example data produces the following plots, so we havent identified any problematic issue...


Thats really odd. Thank you for your help.

Can you list what other libraries you might be loading earlier in the session? I'm pretty new as well, but I know sometimes other libraries can mask functions and features you might be using, it's worth asking just to eliminate that as a possibility.

Hi there. My code is from someone else, and its 2000+ lines. How do I generate the list of libraries that are loaded?

well, you could store the object you want to plot, saveRDS()
then restart your R session (ctrl+shift+f10)
dont run all the old code nor the libraries.
just load the ggplot2 library, and readRDS() and ggplot it...
this would minimise the risk of a collision.

on the other hand if you want to investigate you can do conflicts(), but it could be a lot to go through...

Hit Ctrl-F and search for "library" -- there won't be that many calls.

Thanks for the tip. Should have thought of that earlier.

These are the libraries that are called earlier in the code:

ggplot2
knitr
plotly
ggthemes
naniar
dplyr

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.