I have a shiny app, Ideally, I want a side-by-side boxplot where one of the boxes has all of the data from the selected layer, and the other boxplot shows the subset of the selected layer in comparison on one plot. So, the x-axis would be "All Data" and "Population Name", where "Population Name" changes dynamically (along with the data displayed in the boxplot) based on the user choice. In my ui, the user can choose a layer type AND a population. So, the y-values on the boxplot would change dynamically based on the layer chosen. Here is what my app looks like. I've gotten a dynamic histogram to load, switch, and display correctly:

So, ideally I want a boxplot in place of the histogram. No matter how simple I get with the boxplot...even just displaying all the data and not changing among populations, I cannot get it to display. I don't get any errors, just a lack of plot. What is happening that the boxplots just will not display?? The full code is listed below, then I have a small chunk I have been using instead of the histogram to try and troubleshoot the boxplots, to no avail...
ui <- fluidPage(
titlePanel("Remote sensing products for LCT habitat"),
sidebarLayout(
sidebarPanel(
# Dropdown menu to select polygon layer
selectInput("layer", "Select product:",
choices = c("NDVI status" = "Polygon Layer 1",
"NDVI trend 1985-2021" ="Polygon Layer 2",
"NDVI trend 2017-2021" ="Polygon Layer 3",
"NDVI resilience 2017-2021"="Polygon Layer 4"),
selected = "NDVI status"),
# Dropdown menu to select group
selectInput("Pop_Name", "Select population:",
choices = c("Andorno Creek", "Battle Creek",
"Colman Creek",
"Crowley Creek","Eightmile Creek",
"Falls Canyon Creek", "Jackson Creek",
"Sage Line and Corral Creeks",
"Threemile Creek",
"Washburn Creek"),
selected = "Andorno Creek"),
# Panel for distribution plot
plotOutput("distributionPlot")
),
mainPanel(
leafletOutput("map"), # Output to render the leaflet map
verbatimTextOutput("layerDescription") # Output to display layer description
)
)
)
# Define server logic
server <- function(input, output) {
# Define the descriptions for each layer
layer_descriptions <- c("Polygon Layer 1" = "NDVI status: this layer reflects the mean near difference vegetation index (NDVI)\n value from August 2020 within a valley bottom\n (floodplain) unit. The lower the value, the less riparian\n vegetation in the valley bottom unit, the higher the value,\n the more riparian vegetation in the valley bottom unit. We are \nusing NDVI within the valley bottom as a proxy for floodplain \nconnection: the more green, the more likely the active channel is \nconnected to the floodplain.",
"Polygon Layer 2" = "NDVI trend 1985-2021: this layer reflects the mean Sen's Slope among August near\n difference vegetation index (NDVI)\n from 1985-2021. This shows the relative increase or decrease\n in vegetation greenness over time within the valley bottom\n (floodplain) unit. We are using an increase in greenness as \n a proxy for an increase in floodplain connection. A decrease\n would indicate a decrease in floodplain connection during 1985-2021.",
"Polygon Layer 3" = "NDVI trend 2017-2021: this layer reflects the mean Sen's Slope among August\n near difference vegetation index (NDVI)\n from 1985-2022. This shows the relative increase or decrease\n in vegetation greenness over time within the valley bottom\n (floodplain) unit. We are using an increase in greenness as \n a proxy for an increase in floodplain connection. A decrease\n would indicate a decrease in floodplain connection during 2017-2021.",
"Polygon Layer 4" = "NDVI resilience 2017-2022: this layer reflects the correlation of \nnear difference vegetation index (NDVI)\n and the standardized precipitation evapotranspiration index (SPEI; a drought\n index) from 2017-2022. We are using this as a proxy for resilience to drought.\n If the correlation value is low, than the greenness in the valley bottom unit is not\n affected by drought, and therefore, is resilient. If the correlation value is high,\n than the greenness in the valley bottom unit is affected by drought,\nand therefore, is less resilient.")
# Create a function to assign colors based on cutoffs
assign_colors <- function(data, cutoffs, colors) {
data$color_group <- cut(data$column, breaks = cutoffs, labels = FALSE)
data$fillColor <- colors[data$color_group]
return(data)
}
# Function to render leaflet map
output$map <- renderLeaflet({
# Define cutoffs and corresponding colors for each layer
##Layer 1- Mean august NDVI 2019-2022
cutoffs_layer1 <- c(0,0.3,0.5,0.7)
colors_layer1 <- c("#d2cdb3", "lightgreen", "darkgreen")
##Layer 2- trend 1985-2021
cutoffs_layer2 <- c(-0.0047, -0.0027,-0.0007,0,
0.0033,0.0053,0.0073)
colors_layer2 <- c("#a6611a", "#d4af69", "#ece1c5",
"#c6e5e0","#67bfb1","#018571")
##Layer 3- trend 2017-2021
cutoffs_layer3 <- c(-0.0468010,-0.037,-0.027,
0.0,0.002,0.005,0.013)
colors_layer3 <- c("#a6611a", "#d4af69", "#ece1c5",
"#c6e5e0","#67bfb1","#018571")
##Layer 4-resilience
cutoffs_layer4 <- c(0,0.3,0.5,0.7,1)
colors_layer4 <- c("#b5b645", "#d2cdb3",
"#a4968f","#7a5822")
data <- switch(input$layer,
"Polygon Layer 1" = ndvi_status,
"Polygon Layer 2" = trend_8522,
"Polygon Layer 3" = trend_1322,
"Polygon Layer 4" = resilience_1322)
# Define breaks and colors for the selected layer
breaks <- switch(input$layer,
"Polygon Layer 1" = cutoffs_layer1,
"Polygon Layer 2" = cutoffs_layer2,
"Polygon Layer 3" = cutoffs_layer3,
"Polygon Layer 4" = cutoffs_layer4)
colors <- switch(input$layer,
"Polygon Layer 1" = colors_layer1,
"Polygon Layer 2" = colors_layer2,
"Polygon Layer 3" = colors_layer3,
"Polygon Layer 4" = colors_layer4)
label_layer <- switch(input$layer,
"Polygon Layer 1" = c("0.0-0.3",
"0.3-0.5",
"0.5-0.7"),
"Polygon Layer 2" = c("Most negative NDVI trend: -0.005 - -0.003",
"-0.003 - -0.001",
"-0.001 - 0.000",
"0.000 - 0.003",
"0.003 - 0.005",
"Most positive NDVI trend: 0.005 - 0.007"),
"Polygon Layer 3" = c("Most negative NDVI trend: -0.047 - -0.037",
"-0.037 - -0.027",
"-0.027 - 0.00",
"0.000 - 0.002",
"0.002 - 0.005",
"Most positive NDVI trend: 0.005 - 0.013"),
"Polygon Layer 4" = c("Most resilient: 0.0-0.3",
"Resilient: 0.3-0.5",
"Slightly not resilient: 0.5-0.7",
"Not resilient: 0.7-1.0"))
# Render the leaflet map
leaflet() %>%
addTiles() %>% # Add base tiles
## Add background polygons
addPolygons(data = PRU,
fillOpacity = 0.5,
color = "black",
weight = 1,
popup = ~Pop_Name,
fillColor = "yellow") %>%
# Add polygons based on the selected layer
addPolygons(data = switch(input$layer,
"Polygon Layer 1" = assign_colors(ndvi_status, cutoffs_layer1, colors_layer1),
"Polygon Layer 2" = assign_colors(trend_8522, cutoffs_layer2, colors_layer2),
"Polygon Layer 3" = assign_colors(trend_1322, cutoffs_layer3, colors_layer3),
"Polygon Layer 4" = assign_colors(resilience_1322, cutoffs_layer4, colors_layer4)),
fillOpacity = 0.8,
color = "black",
weight = 1,
popup = ~paste("Value:", column),
fillColor = ~fillColor) %>%
# Add legend dynamically
addLegend(position = "bottomright",
colors = colors,
labels = label_layer,
title = "Legend",
opacity = 1)
})
# Render the layer description based on selected layer
output$layerDescription <- renderText({
selected_layer <- input$layer
layer_descriptions[selected_layer]
})
# Render distribution plot based on selected layer
output$distributionPlot <- renderPlot({
data <- switch(input$layer,
"Polygon Layer 1" = ndvi_status,
"Polygon Layer 2" = trend_8522,
"Polygon Layer 3" = trend_1322,
"Polygon Layer 4" = resilience_1322)
ggplot(data, aes(x = column)) +
geom_histogram(aes(fill = ifelse(Pop_Name == input$Pop_Name, "Selected Group", "All Data")),
color = "black", bins = 30, alpha = 0.6) +
labs(title = "Histogram Plot",
x = "Column Name",
fill = "Population") +
theme_minimal() +
xlim(range(data$column)) + # Set x-axis limits based on the range of data$column
scale_fill_manual(values = c("skyblue", "green"),
labels = c("All Data", input$Pop_Name))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Boxplot code
# Render grouped boxplot based on selected layer
output$groupedBoxplot <- renderPlot({
data <- switch(input$layer,
"Polygon Layer 1" = ndvi_status,
"Polygon Layer 2" = trend_8522,
"Polygon Layer 3" = trend_1322,
"Polygon Layer 4" = resilience_1322)
# Create a grouped boxplot
ggplot(data, aes(x = Pop_Name, y = column, fill = Pop_Name)) +
geom_boxplot() +
labs(title = "Grouped Boxplot",
x = "Pop_Name",
y = "Value",
fill = "Pop_Name") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
})