Issue with Separating a Single Plot from a Figure of Plots using facet_wrap()

I have successfully created multiple bar charts in one figure that illustrates ozone concentrations for various cities in North Carolina. Now, I want to separate certain plots from this figure so only a single plot is shown. For example, rather than the output being the figure with each plot for each city, I'd rather have the output be the single bar plot for Asheville or Cullowhee. I was told facet_wrap() can help with this but have had no luck. Any help would be much appreciated!

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

### Sample Data Set
data.frame(stringsAsFactors=FALSE,
              data_cbsa = c("Asheville, NC", "Asheville, NC", "Asheville, NC",
                            "Asheville, NC", "Asheville, NC", "Asheville, NC",
                            "Asheville, NC", "Asheville, NC", "Asheville, NC",
                            "Asheville, NC", "Asheville, NC", "Asheville, NC",
                            "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia,
                            NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC",
                            "Charlotte-Concord-Gastonia, NC-SC", "Cullowhee, NC", "Cullowhee, NC",
                            "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                            "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                            "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                            "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                            "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                            "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                            "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                            "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                            "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC", "Fayetteville,
                            NC", "Fayetteville, NC", "Fayetteville, NC",
                            "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC",
                            "Fayetteville, NC", "Fayetteville, NC", "Fayetteville,
                            NC", "Fayetteville, NC", "Fayetteville, NC",
                            "Fayetteville, NC", "Greensboro-High Point, NC",
                            "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                            "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                            "Greensboro-High Point, NC", "Greensboro-High Point,
                            NC", "Greensboro-High Point, NC",
                            "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                            "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                            "Greenville, NC", "Greenville, NC", "Greenville, NC",
                            "Greenville, NC", "Greenville, NC", "Greenville, NC",
                            "Greenville, NC", "Greenville, NC", "Greenville, NC",
                            "Greenville, NC", "Greenville, NC", "Greenville, NC",
                            "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton,
                            NC", "Hickory-Lenoir-Morganton, NC",
                            "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                            "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton,
                            NC", "Hickory-Lenoir-Morganton, NC",
                            "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                            "Hickory-Lenoir-Morganton, NC",
                            "Hickory-Lenoir-Morganton, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC",
                            "Kinston, NC", "Kinston, NC", "Kinston, NC", "Kinston,
                            NC", "Kinston, NC", "Kinston, NC", "Kinston, NC",
                            "Kinston, NC", "Kinston, NC", "Morehead City, NC",
                            "Morehead City, NC", "Morehead City, NC", "Morehead City,
                            NC", "Morehead City, NC", "Morehead City, NC",
                            "Morehead City, NC", "Morehead City, NC", "Morehead City,
                            NC", "Morehead City, NC", "Morehead City, NC",
                            "Morehead City, NC", "Oxford, NC", "Oxford, NC", "Oxford,
                            NC", "Oxford, NC", "Oxford, NC", "Oxford, NC",
                            "Oxford, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC",
                            "Oxford, NC", "Oxford, NC", "Raleigh, NC", "Raleigh, NC",
                            "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                            "Raleigh, NC", "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                            "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                            "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                            "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                            "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount,
                            NC", "Rocky Mount, NC", "Rocky Mount, NC",
                            "Rocky Mount, NC", "Sanford, NC", "Sanford, NC", "Sanford, NC",
                            "Sanford, NC", "Sanford, NC", "Wilmington, NC",
                            "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                            "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                            "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                            "Wilmington, NC", "Wilmington, NC", "Winston-Salem,
                            NC", "Winston-Salem, NC", "Winston-Salem, NC",
                            "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC",
                            "Winston-Salem, NC", "Winston-Salem, NC",
                            "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC"),
              data_year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                            2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010,
                            2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                            2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
                            2018, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                            2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010, 2011,
                            2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                            2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
                            2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
                            2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010,
                            2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                            2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
                            2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                            2013, 2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009,
                            2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
                            2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
                            2016, 2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                            2013, 2014, 2015, 2016, 2017, 2018, 2014, 2015, 2016,
                            2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                            2013, 2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009,
                            2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017),
   highest_4th_max_cbsa = c(0.077, 0.08, 0.066, 0.074, 0.067, 0.07, 0.064,
                            0.067, 0.066, 0.067, 0.063, 0.065, 0.096, 0.093,
                            0.073, 0.082, 0.088, 0.085, 0.067, 0.068, 0.073, 0.074,
                            0.068, 0.07, 0.08, 0.063, 0.069, 0.068, 0.069,
                            0.067, 0.068, 0.065, 0.069, 0.064, 0.065, 0.08, 0.078,
                            0.066, 0.074, 0.072, 0.076, 0.062, 0.065, 0.061, 0.063,
                            0.061, 0.063, 0.082, 0.075, 0.068, 0.073, 0.076,
                            0.069, 0.062, 0.066, 0.062, 0.064, 0.063, 0.064, 0.086,
                            0.084, 0.072, 0.076, 0.076, 0.078, 0.062, 0.065,
                            0.066, 0.068, 0.065, 0.067, 0.079, 0.077, 0.066, 0.069,
                            0.074, 0.071, 0.063, 0.063, 0.06, 0.065, 0.061, 0.066,
                            0.081, 0.076, 0.064, 0.071, 0.067, 0.067, 0.063,
                            0.064, 0.065, 0.066, 0.063, 0.065, 0.077, 0.074, 0.064,
                            0.069, 0.069, 0.069, 0.063, 0.065, 0.062, 0.063,
                            0.062, 0.064, 0.07, 0.059, 0.062, 0.065, 0.063, 0.058,
                            0.062, 0.062, 0.058, 0.06, 0.057, 0.06, 0.083, 0.081,
                            0.07, 0.074, 0.072, 0.072, 0.063, 0.065, 0.063, 0.065,
                            0.065, 0.065, 0.084, 0.078, 0.069, 0.074, 0.078,
                            0.075, 0.062, 0.064, 0.065, 0.069, 0.066, 0.063, 0.08,
                            0.075, 0.066, 0.072, 0.072, 0.071, 0.064, 0.062, 0.061,
                            0.064, 0.061, 0.062, 0.064, 0.06, 0.064, 0.059,
                            0.06, 0.071, 0.066, 0.06, 0.062, 0.064, 0.064, 0.064,
                            0.063, 0.057, 0.06, 0.057, 0.062, 0.085, 0.081, 0.068,
                            0.081, 0.076, 0.079, 0.066, 0.067, 0.07, 0.07, 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", fill = 'red') +
  facet_wrap(~data_cbsa, 'Asheville, NC')
Warning messages:
  1: Coercing `nrow` to be an integer. 
2: In sanitise_dim(nrow) : NAs introduced by coercion
3: `nrow` is missing or less than 1 and will be treated as NULL.

If you want only one plot, do you really need to use facet_wrap? You can make the plot based on a subset of the dataset. For example, run this:

o3_cbsa_annual_2015_std_summary <- data.frame(stringsAsFactors=FALSE,
                 data_cbsa = c("Asheville, NC", "Asheville, NC", "Asheville, NC",
                               "Asheville, NC", "Asheville, NC", "Asheville, NC",
                               "Asheville, NC", "Asheville, NC", "Asheville, NC",
                               "Asheville, NC", "Asheville, NC", "Asheville, NC",
                               "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC",
                               "Charlotte-Concord-Gastonia, NC-SC", "Cullowhee, NC", "Cullowhee, NC",
                               "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                               "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                               "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                               "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                               "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                               "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                               "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                               "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                               "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC", "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC",
                               "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC",
                               "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC",
                               "Fayetteville, NC", "Greensboro-High Point, NC",
                               "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                               "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                               "Greensboro-High Point, NC", "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                               "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                               "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                               "Greenville, NC", "Greenville, NC", "Greenville, NC",
                               "Greenville, NC", "Greenville, NC", "Greenville, NC",
                               "Greenville, NC", "Greenville, NC", "Greenville, NC",
                               "Greenville, NC", "Greenville, NC", "Greenville, NC",
                               "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                               "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                               "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                               "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                               "Hickory-Lenoir-Morganton, NC",
                               "Hickory-Lenoir-Morganton, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC",
                               "Kinston, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC",
                               "Kinston, NC", "Kinston, NC", "Morehead City, NC",
                               "Morehead City, NC", "Morehead City, NC", "Morehead City, NC", "Morehead City, NC", "Morehead City, NC",
                               "Morehead City, NC", "Morehead City, NC", "Morehead City, NC", "Morehead City, NC", "Morehead City, NC",
                               "Morehead City, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC",
                               "Oxford, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC",
                               "Oxford, NC", "Oxford, NC", "Raleigh, NC", "Raleigh, NC",
                               "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                               "Raleigh, NC", "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                               "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                               "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                               "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                               "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                               "Rocky Mount, NC", "Sanford, NC", "Sanford, NC", "Sanford, NC",
                               "Sanford, NC", "Sanford, NC", "Wilmington, NC",
                               "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                               "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                               "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                               "Wilmington, NC", "Wilmington, NC", "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC",
                               "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC",
                               "Winston-Salem, NC", "Winston-Salem, NC",
                               "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC"),
                 data_year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                               2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010,
                               2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                               2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
                               2018, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                               2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010, 2011,
                               2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                               2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
                               2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
                               2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010,
                               2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                               2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
                               2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                               2013, 2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009,
                               2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
                               2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
                               2016, 2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                               2013, 2014, 2015, 2016, 2017, 2018, 2014, 2015, 2016,
                               2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                               2013, 2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009,
                               2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017),
                 highest_4th_max_cbsa = c(0.077, 0.08, 0.066, 0.074, 0.067, 0.07, 0.064,
                                          0.067, 0.066, 0.067, 0.063, 0.065, 0.096, 0.093,
                                          0.073, 0.082, 0.088, 0.085, 0.067, 0.068, 0.073, 0.074,
                                          0.068, 0.07, 0.08, 0.063, 0.069, 0.068, 0.069,
                                          0.067, 0.068, 0.065, 0.069, 0.064, 0.065, 0.08, 0.078,
                                          0.066, 0.074, 0.072, 0.076, 0.062, 0.065, 0.061, 0.063,
                                          0.061, 0.063, 0.082, 0.075, 0.068, 0.073, 0.076,
                                          0.069, 0.062, 0.066, 0.062, 0.064, 0.063, 0.064, 0.086,
                                          0.084, 0.072, 0.076, 0.076, 0.078, 0.062, 0.065,
                                          0.066, 0.068, 0.065, 0.067, 0.079, 0.077, 0.066, 0.069,
                                          0.074, 0.071, 0.063, 0.063, 0.06, 0.065, 0.061, 0.066,
                                          0.081, 0.076, 0.064, 0.071, 0.067, 0.067, 0.063,
                                          0.064, 0.065, 0.066, 0.063, 0.065, 0.077, 0.074, 0.064,
                                          0.069, 0.069, 0.069, 0.063, 0.065, 0.062, 0.063,
                                          0.062, 0.064, 0.07, 0.059, 0.062, 0.065, 0.063, 0.058,
                                          0.062, 0.062, 0.058, 0.06, 0.057, 0.06, 0.083, 0.081,
                                          0.07, 0.074, 0.072, 0.072, 0.063, 0.065, 0.063, 0.065,
                                          0.065, 0.065, 0.084, 0.078, 0.069, 0.074, 0.078,
                                          0.075, 0.062, 0.064, 0.065, 0.069, 0.066, 0.063, 0.08,
                                          0.075, 0.066, 0.072, 0.072, 0.071, 0.064, 0.062, 0.061,
                                          0.064, 0.061, 0.062, 0.064, 0.06, 0.064, 0.059,
                                          0.06, 0.071, 0.066, 0.06, 0.062, 0.064, 0.064, 0.064,
                                          0.063, 0.057, 0.06, 0.057, 0.062, 0.085, 0.081, 0.068,
                                          0.081, 0.076, 0.079, 0.066, 0.067, 0.07, 0.07, 0.066)
)

library(ggplot2)

ggplot(data = subset(x = o3_cbsa_annual_2015_std_summary,
                     data_cbsa == 'Asheville, NC'),
       mapping = aes(x = data_year,
                     y = highest_4th_max_cbsa)) +
  geom_col(fill = 'red')

Created on 2019-08-16 by the reprex package (v0.3.0)

For your future posts, please provide a smaller dataset sufficient enough to reproduce the problem. Also, please include the library calls.

1 Like

facet_wrap() generates a "facet" (or subplot) for each level of a categorical variable, see this example.

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

### Sample Data Set
o3_cbsa_annual_2015_std_summary <- data.frame(stringsAsFactors=FALSE,
           data_cbsa = c("Asheville, NC", "Asheville, NC", "Asheville, NC",
                         "Asheville, NC", "Asheville, NC", "Asheville, NC",
                         "Asheville, NC", "Asheville, NC", "Asheville, NC",
                         "Asheville, NC", "Asheville, NC", "Asheville, NC",
                         "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia,
                            NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC", "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC",
                         "Charlotte-Concord-Gastonia, NC-SC", "Cullowhee, NC", "Cullowhee, NC",
                         "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                         "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                         "Cullowhee, NC", "Cullowhee, NC", "Cullowhee, NC",
                         "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                         "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                         "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                         "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                         "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC",
                         "Durham-Chapel Hill, NC", "Durham-Chapel Hill, NC", "Fayetteville,
                            NC", "Fayetteville, NC", "Fayetteville, NC",
                         "Fayetteville, NC", "Fayetteville, NC", "Fayetteville, NC",
                         "Fayetteville, NC", "Fayetteville, NC", "Fayetteville,
                            NC", "Fayetteville, NC", "Fayetteville, NC",
                         "Fayetteville, NC", "Greensboro-High Point, NC",
                         "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                         "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                         "Greensboro-High Point, NC", "Greensboro-High Point,
                            NC", "Greensboro-High Point, NC",
                         "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                         "Greensboro-High Point, NC", "Greensboro-High Point, NC",
                         "Greenville, NC", "Greenville, NC", "Greenville, NC",
                         "Greenville, NC", "Greenville, NC", "Greenville, NC",
                         "Greenville, NC", "Greenville, NC", "Greenville, NC",
                         "Greenville, NC", "Greenville, NC", "Greenville, NC",
                         "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton,
                            NC", "Hickory-Lenoir-Morganton, NC",
                         "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                         "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton,
                            NC", "Hickory-Lenoir-Morganton, NC",
                         "Hickory-Lenoir-Morganton, NC", "Hickory-Lenoir-Morganton, NC",
                         "Hickory-Lenoir-Morganton, NC",
                         "Hickory-Lenoir-Morganton, NC", "Kinston, NC", "Kinston, NC", "Kinston, NC",
                         "Kinston, NC", "Kinston, NC", "Kinston, NC", "Kinston,
                            NC", "Kinston, NC", "Kinston, NC", "Kinston, NC",
                         "Kinston, NC", "Kinston, NC", "Morehead City, NC",
                         "Morehead City, NC", "Morehead City, NC", "Morehead City,
                            NC", "Morehead City, NC", "Morehead City, NC",
                         "Morehead City, NC", "Morehead City, NC", "Morehead City,
                            NC", "Morehead City, NC", "Morehead City, NC",
                         "Morehead City, NC", "Oxford, NC", "Oxford, NC", "Oxford,
                            NC", "Oxford, NC", "Oxford, NC", "Oxford, NC",
                         "Oxford, NC", "Oxford, NC", "Oxford, NC", "Oxford, NC",
                         "Oxford, NC", "Oxford, NC", "Raleigh, NC", "Raleigh, NC",
                         "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                         "Raleigh, NC", "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                         "Raleigh, NC", "Raleigh, NC", "Raleigh, NC",
                         "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                         "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount, NC",
                         "Rocky Mount, NC", "Rocky Mount, NC", "Rocky Mount,
                            NC", "Rocky Mount, NC", "Rocky Mount, NC",
                         "Rocky Mount, NC", "Sanford, NC", "Sanford, NC", "Sanford, NC",
                         "Sanford, NC", "Sanford, NC", "Wilmington, NC",
                         "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                         "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                         "Wilmington, NC", "Wilmington, NC", "Wilmington, NC",
                         "Wilmington, NC", "Wilmington, NC", "Winston-Salem,
                            NC", "Winston-Salem, NC", "Winston-Salem, NC",
                         "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC",
                         "Winston-Salem, NC", "Winston-Salem, NC",
                         "Winston-Salem, NC", "Winston-Salem, NC", "Winston-Salem, NC"),
           data_year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                         2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010,
                         2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                         2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
                         2018, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                         2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010, 2011,
                         2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                         2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
                         2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
                         2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009, 2010,
                         2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2007,
                         2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
                         2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                         2013, 2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009,
                         2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
                         2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
                         2016, 2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                         2013, 2014, 2015, 2016, 2017, 2018, 2014, 2015, 2016,
                         2017, 2018, 2007, 2008, 2009, 2010, 2011, 2012,
                         2013, 2014, 2015, 2016, 2017, 2018, 2007, 2008, 2009,
                         2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017),
           highest_4th_max_cbsa = c(0.077, 0.08, 0.066, 0.074, 0.067, 0.07, 0.064,
                                    0.067, 0.066, 0.067, 0.063, 0.065, 0.096, 0.093,
                                    0.073, 0.082, 0.088, 0.085, 0.067, 0.068, 0.073, 0.074,
                                    0.068, 0.07, 0.08, 0.063, 0.069, 0.068, 0.069,
                                    0.067, 0.068, 0.065, 0.069, 0.064, 0.065, 0.08, 0.078,
                                    0.066, 0.074, 0.072, 0.076, 0.062, 0.065, 0.061, 0.063,
                                    0.061, 0.063, 0.082, 0.075, 0.068, 0.073, 0.076,
                                    0.069, 0.062, 0.066, 0.062, 0.064, 0.063, 0.064, 0.086,
                                    0.084, 0.072, 0.076, 0.076, 0.078, 0.062, 0.065,
                                    0.066, 0.068, 0.065, 0.067, 0.079, 0.077, 0.066, 0.069,
                                    0.074, 0.071, 0.063, 0.063, 0.06, 0.065, 0.061, 0.066,
                                    0.081, 0.076, 0.064, 0.071, 0.067, 0.067, 0.063,
                                    0.064, 0.065, 0.066, 0.063, 0.065, 0.077, 0.074, 0.064,
                                    0.069, 0.069, 0.069, 0.063, 0.065, 0.062, 0.063,
                                    0.062, 0.064, 0.07, 0.059, 0.062, 0.065, 0.063, 0.058,
                                    0.062, 0.062, 0.058, 0.06, 0.057, 0.06, 0.083, 0.081,
                                    0.07, 0.074, 0.072, 0.072, 0.063, 0.065, 0.063, 0.065,
                                    0.065, 0.065, 0.084, 0.078, 0.069, 0.074, 0.078,
                                    0.075, 0.062, 0.064, 0.065, 0.069, 0.066, 0.063, 0.08,
                                    0.075, 0.066, 0.072, 0.072, 0.071, 0.064, 0.062, 0.061,
                                    0.064, 0.061, 0.062, 0.064, 0.06, 0.064, 0.059,
                                    0.06, 0.071, 0.066, 0.06, 0.062, 0.064, 0.064, 0.064,
                                    0.063, 0.057, 0.06, 0.057, 0.062, 0.085, 0.081, 0.068,
                                    0.081, 0.076, 0.079, 0.066, 0.067, 0.07, 0.07, 0.066)
)

### Minimal Runnable Code 
o3_cbsa_annual_2015_std_summary %>% 
    filter(data_cbsa %in% c("Asheville, NC", "Cullowhee, NC")) %>% # Filter desired values.
    ggplot(aes(x = data_year, y = highest_4th_max_cbsa)) + 
    geom_col(position = "dodge", fill = 'red') +
    facet_wrap(~data_cbsa) # Generate sub plots for each level of the categorical variable.

Created on 2019-08-16 by the reprex package (v0.3.0.9000)

2 Likes

A line plot might work better here. For example:

library(tidyverse)

o3_cbsa_annual_2015_std_summary %>% 
  # Remove state abbreviations from metro area names
  mutate(data_cbsa = gsub(",.*", "", data_cbsa)) %>% 
  # Set metro area order from highest to lowest 2018 ozone levels
  arrange(desc(data_year), desc(highest_4th_max_cbsa)) %>% 
  mutate(data_cbsa = factor(data_cbsa, levels=unique(data_cbsa))) %>% 
  ggplot(aes(x=data_year,y=highest_4th_max_cbsa, colour=data_cbsa)) + 
    geom_hline(yintercept=0.070, linetype="11", colour="red") +
    geom_line() +
    expand_limits(y=0) +
    scale_y_continuous(expand=expand_scale(mult=c(0,0.02))) +
    scale_x_continuous(breaks=seq(2008,2020,2)) +
    theme_minimal() +
    scale_colour_viridis_d(end=0.9) +
    labs(y="4th max (ppm)",
         x="Year", 
         colour="Metro Area")

Rplot01

Or if that's too busy:

o3_cbsa_annual_2015_std_summary %>% 
  mutate(data_cbsa = gsub(",.*", "", data_cbsa)) %>% 
  ggplot(aes(x=data_year,y=highest_4th_max_cbsa)) + 
  geom_hline(yintercept=0.070, linetype="11", colour="red") +
    geom_line() +
    expand_limits(y=0) +
    scale_y_continuous(expand=expand_scale(mult=c(0,0.04))) +
    scale_x_continuous(breaks=seq(2008,2020,2)) +
    theme_bw() +
    theme(axis.text.x=element_text(angle=-90, vjust=0.5)) +
    labs(y="4th max (ppm)", x="Year") +
    facet_wrap(~data_cbsa)

Or you could go super-minimal:

library(ggthemes)

o3_cbsa_annual_2015_std_summary %>% 
  mutate(data_cbsa = gsub(",.*", "", data_cbsa)) %>% 
  arrange(desc(data_year), desc(highest_4th_max_cbsa)) %>% 
  mutate(data_cbsa = factor(data_cbsa, levels=unique(data_cbsa))) %>% 
  ggplot(aes(x=data_year,y=highest_4th_max_cbsa)) + 
    geom_text(data=. %>% group_by(data_cbsa) %>% slice(1), 
              aes(label=str_wrap(data_cbsa, 17)), 
              y=0.01, x=2012, size=2.8, family="serif", vjust=0) +
    geom_hline(yintercept=0.070, linetype="11", colour="red") +
    geom_hline(yintercept=0, size=0.3, colour="grey60") +
    geom_line() +
    expand_limits(y=0) +
    scale_y_continuous(expand=expand_scale(mult=c(0,0.02))) +
    scale_x_continuous(breaks=seq(2008,2020,4)) +
    theme_tufte() +
    theme(axis.text.x=element_text(angle=-90, vjust=0.5, size=rel(0.8)),
          axis.ticks.x=element_line(colour="grey50"),
          axis.title.x=element_blank(), 
          strip.text=element_blank()) +
    labs(y="4th max (ppm)") +
    facet_wrap(~data_cbsa, nrow=2)

And, of course, you can show subsets, as demonstrated in the other responses.

2 Likes

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