Shiny and ggplot2 with multiple lines

Viewed 874

I'm new to Shiny and have run into problems when I try to render a ggplot. I want to render a plot with multiple lines but I get the error: Warning: Error in : Aesthetics must be either length 1 or the same as the data (1)

It work fine when I render a single line, but not multiple. There are earlier questions on Stack Overflow adressing similar issues, but I'm afraid I dont fully understand their soulutions.

Help would be much appreciated. :)

library(tidyverse)
library(shiny)

url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-18"), ".xlsx", sep = "")
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
df <- read_excel(tf)

df  <- df %>%
        rename(country = countriesAndTerritories) %>% 
        arrange(country, dateRep) %>%
        group_by(country) %>%
        mutate(Cumulative_Death = cumsum(deaths)) %>%
        ungroup() %>%
        filter(Cumulative_Death > 9) %>%
        group_by(country) %>%
        mutate(numbers_of_days = row_number(),
               First_Death_Date = min(dateRep)) %>% 
        select(country, numbers_of_days, deaths, Cumulative_Death)

ui <- fluidPage(
        titlePanel("Statistik Covid-19"),
        sidebarLayout(
                sidebarPanel(
                selectInput("cou", "Country:", choices = unique(df$country), selected = "SWeden", multiple = TRUE),
                selectInput("var", "Variable:", choices = c("deaths", "Cumulative_Death"))),
        mainPanel(
                plotOutput("covid"))
        ))

server <- function(input, output, session){

        selected <- reactive(filter(df, country %in% input$cou))

        output$covid <- renderPlot({
                ggplot(selected(), aes(x=numbers_of_days, input$var, colour = input$cou)) +
                        geom_line(size = 1.5) +
                        labs(title = "Covid-19: Antal döda per 100 000 invånare",
                             x = "DAGAR SEDAN ANTAL DÖDSFALL ÖVERSTEG TIO",
                             y = paste0(input$var),
                             caption = "Source: European Centre for Disease Prevention and Control") +
                        guides(colour = guide_legend(title=NULL))
        })
        }

shinyApp(ui, server)
1 Answers

Try this. As @SusanSwitzer already mentioned. Main issue is that you use input$land. So. Simply replace with input$country. Second. Map colour on country which is the varname in the df. Third I switched to aes_string instead of aes to use the character inputs:

library(shiny)
library(ggplot2)
library(dplyr)

url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-18"), ".xlsx", sep = "")
httr::GET(url, httr::authenticate(":", ":", type="ntlm"), httr::write_disk(tf <- tempfile(fileext = ".xlsx")))
df <- readxl::read_excel(tf)

df  <- df %>%
  rename(country = countriesAndTerritories) %>% 
  arrange(country, dateRep) %>%
  group_by(country) %>%
  mutate(Cumulative_Death = cumsum(deaths)) %>%
  ungroup() %>%
  filter(Cumulative_Death > 9) %>%
  group_by(country) %>%
  mutate(numbers_of_days = row_number(),
         First_Death_Date = min(dateRep)) %>% 
  select(country, numbers_of_days, deaths, Cumulative_Death)

ui <- fluidPage(
  titlePanel("Statistik Covid-19"),
  sidebarLayout(
    sidebarPanel(
      selectInput("country", "Country:", choices = unique(df$country), selected = "Sweden", multiple = TRUE),
      selectInput("var", "Variable:", choices = c("deaths", "Cumulative_Death"))),
    mainPanel(
      plotOutput("covid"))
  ))

server <- function(input, output, session){

  # input$country instead input$land
  selected <- reactive(filter(df, country %in% input$country))

  output$covid <- renderPlot({
    # switch to aes_string. map colour on country instead of input$land
    ggplot(selected(), aes_string(x = "numbers_of_days", y = input$var, colour = "country")) + 
      geom_line(size = 1.5) +
      labs(title = "Covid-19: Antal döda per 100 000 invånare",
           x = "DAGAR SEDAN ANTAL DÖDSFALL ÖVERSTEG TIO",
           y = paste0(input$var),
           caption = "Source: European Centre for Disease Prevention and Control") +
      guides(colour = guide_legend(title=NULL))
  })
}

shinyApp(ui, server)
Related