I'm trying to create a ggplot 2 choropleth when the user inputs specific data in my RShiny app. I merged my spatial dataframe with my data as shown below, and I think that worked properly:
# inputting data in RShiny app:
output$contents <- renderTable({
file <- input$uploaded_data
ext <- tools::file_ext(file$datapath)
req(file)
counties_and_trash <- read_csv(file$datapath)
mean_each_type <- counties_and_trash %>%
group_by(COUNTY) %>%
summarise(mean_plastics = mean(PLASTICS, na.rm = TRUE),
mean_papers = mean(PAPERS, na.rm = TRUE),
mean_metals = mean(METALS, na.rm = TRUE))
})
# merging spdf and data:
spdf <- geojson_read('County_Boundaries_of_NJ.geojson', what = "sp")
merged <- merge(spdf, mean_each_type, by.x = 'COUNTY', by.y = 'mean_plastics')
Please note that, as of right now, I'm only trying to plot "mean_plastics" in the choropleth. The other ones ("mean_papers" and "mean_metals") are irrelevant at the moment. I tried to output the ggplot2 choropleth plot in the RShiny app, as shown below:
output$plot <- renderPlot({
ggplot(merged, aes(x = x, y = y)) +
geom_polygon(aes(fill = mean_plastics), data = mean_each_type, x = 'COUNTY', y = 'mean_plastics') +
theme_void() +
coord_map()
})
When I try to output that, I get the following error:
Warning: Error in geom_polygon: Problem while converting geom to grob.
ℹ Error occurred in the 1st layer.
Caused by error in `lat * pi`:
! non-numeric argument to binary operator
I'm certain there's something wrong with the "output$plot..." block of code, but I'm not sure how to fix it.
If more information is needed, I can provide it. I apologize if I didn't explain it well enough, this is my first time doing a more complicated R project. Thank you for any and all help!
EDIT: I've added the minimal reproducible example below.
The only file you'll have to download for the code to work is at the following link (the downloaded file should have the name "County_Boundaries_of_NJ.geojson"):
download link for NJ county boundaries geojson
Here's the minimal reproducible code:
library(shiny)
library(tidyverse)
library(ggplot2)
library(geojsonio)
library(broom)
library(sp)
library(sf)
ui <- fluidPage(
fluidRow(
h1(strong('NJ Beachsweep Mapping with R'), align = 'center'),
column(6,
# this empty column is just to put the other column in the right place
),
column(6,
tableOutput("contents")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output, session) {
counties_and_trash <- structure(list(COUNTY = c("Atlantic", "Atlantic", "Bergen", "Burlington",
"Burlington", "Essex", "Middlesex", "Cape May", "Cape May", "Cape May",
"Monmouth", "Monmouth", "Monmouth", "Monmouth", "Monmouth", "Monmouth",
"Ocean"), PLASTICS = c(340, 300, 325, 467, 545, 354, 433, 325,
324, 653, 768, 457, 486, 944, 356, 457, 568), PAPERS = c(260,
210, 453, 223, 235, 356, 324, 274, 540, 346, 475, 462, 342, 354,
435, 346, 234), METALS = c(45, 35, 123, 124, 224, 124, 134, 342,
230, 243, 324, 125, 323, 122, 334, 421, 401)), row.names = c(NA,
-17L), spec = structure(list(cols = list(COUNTY = structure(list(), class = c("collector_character",
"collector")), PLASTICS = structure(list(), class = c("collector_double",
"collector")), PAPERS = structure(list(), class = c("collector_double",
"collector")), METALS = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
mean_each_type <- counties_and_trash %>%
group_by(COUNTY) %>%
summarise(mean_plastics = mean(PLASTICS, na.rm = TRUE),
mean_papers = mean(PAPERS, na.rm = TRUE),
mean_metals = mean(METALS, na.rm = TRUE))
output$contents <- renderTable({
counties_and_trash
})
spdf <- geojson_read('County_Boundaries_of_NJ.geojson', what = "sp")
merged <- merge(spdf, mean_each_type, by.x = 'COUNTY', by.y = 'mean_plastics')
output$plot <- renderPlot({ # this block of code is where the issue most likely is
ggplot(merged, aes(x = x, y = y)) +
geom_polygon(aes(fill = mean_plastics)) +
theme_void() +
coord_map()
})
}
shinyApp(ui = ui, server = server)
Thank you so much!