Formatting Issue with Creating Un-Stacked Bar Plot using ggplot

I'm currently working with air quality data and attempting to create a simple bar chart showing locations, such as Asheville, Cullowhee, Raleigh, etc, on the x-axis and ozone concentration on the Y axis. My current code creates a stacked bar chart. Here is my code that's generating the stacked bar plot and the error message:

ggplot(o3_cbsa_annual_2015_std_summary,aes(x=data_year,y=highest_4th_max_cbsa,color=data_cbsa)) +
+   geom_bar(stat = "identity")
>   geom_col(aes(fill = keys)) + 
+   facet_wrap(~Subject, ncol = 4)
Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

I would appreciate any help with this issue! Thank you for your time and have a nice day!

First issue is to add all the elements together, but using geom_bar and geom_col doesn't make sense. Also you probably don't require aes(color) and aes(fill).

Try this:

ggplot(o3_cbsa_annual_2015_std_summary, 
       aes(x=data_year, y=highest_4th_max_cbsa)) + 
  geom_col(aes(fill = keys)) + 
  facet_wrap(~Subject, ncol = 4)

About the "un-stacked" part of the request, you can specify the desired position of the columns, like this:

geom_col(position = "dodge")

Thank you both for the response! I reformatted the code to your suggestions and now have run into another issue of a subject missing in the plot and layer 1. Here is the code and error message:

ggplot(o3_cbsa_annual_2015_std_summary, 
+        aes(x=data_year, y=highest_4th_max_cbsa)) + 
+   geom_col(position = "dodge", aes(fill = keys)) + 
+   facet_wrap(~Subject, ncol = 4)
Error: At least one layer must contain all faceting variables: `Subject`.
* Plot is missing `Subject`
* Layer 1 is missing `Subject`

My apologies for the simple questions. Any help with this issue would be much appreciated!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

I believe I have followed the instructions to create a reprex of my code. If something is wrong feel free to let me know! I apologize for any mistakes with the reprex, this is my first time producing one. I have also provided extra information at the bottom to assist with any confusion.

#Libraries Used
library(httr)
library(jsonlite)
library(ggplot2)
library(dplyr)

#Sample Data Set
data.frame(stringsAsFactors=FALSE,
              data_cbsa = c("Asheville, NC", "Charlotte-Concord-Gastonia,
                            NC-SC", "Cullowhee, NC", "Durham-Chapel Hill, NC",
                            "Fayetteville, NC", "Greensboro-High Point, NC",
                            "Greenville, NC", "Hickory-Lenoir-Morganton, NC",
                            "Kinston, NC", "Morehead City, NC", "Oxford, NC", "Raleigh,
                            NC", "Rocky Mount, NC", "Sanford, NC", "Wilmington,
                            NC", "Winston-Salem, NC"),
              data_year = c(2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
                            2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017),
   highest_4th_max_cbsa = c(0.063, 0.068, 0.064, 0.061, 0.063, 0.065, 0.061,
                            0.063, 0.062, 0.057, 0.065, 0.066, 0.061, 0.059,
                            0.057, 0.066)
#Minimal Runnable Code
> ggplot(o3_cbsa_annual_2015_std_summary, 
+        aes(x=data_year, y=highest_4th_max_cbsa)) + 
+   geom_col(position = "dodge", aes(fill = keys)) + 
+   facet_wrap(~Subject, ncol = 4)
Error: At least one layer must contain all faceting variables: `Subject`.
* Plot is missing `Subject`
* Layer 1 is missing `Subject`

#Extra Info
Data was acquired from an online server that required a username and password
data_mart_api_user<- "" #enter username
data_mart_api_password<- "" #enter password

my_state<-"37"
my_state_name<-"North Carolina"

begin_date<-"20170101"
end_date<-"20171231"
base_url<-"https://aqs.epa.gov/data/api/"
request_type<-"monitors/byState?"
my_param<-"44201"

You are almost there, but unfotunatley your sample data is not complete enough to reproduce your issue, since it doesn´t contain the variables keys and Subject which you are trying to use in your code.
Does your actual data contain this variables? if the answer is no, then that would be the cause of the error message you are getting.

Dear Andres,

The variables keys and Subjects were not part of my data. Fortunately, I got the ggplot code to work and it generated bar plots like I wanted. Thanks for the help!

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