【问题标题】:Shiny and ggplot2 with multiple lines多行的闪亮和 ggplot2
【发布时间】:2020-06-18 20:42:55
【问题描述】:

我是 Shiny 的新手,在尝试渲染 ggplot 时遇到了问题。我想用多条线渲染一个图,但出现错误:警告:错误:美学必须是长度 1 或与数据相同 (1)

当我渲染单行而不是多行时它工作正常。 Stack Overflow 上早有一些针对类似问题的问题,但恐怕我不完全理解他们的解决方案。

我们将不胜感激。 :)

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)

【问题讨论】:

  • 您正在服务器中调用 input$land,但 UI 中没有土地参考。请列出您正在使用的库以加快支持此查询。

标签: r ggplot2 shiny tidyverse


【解决方案1】:

试试这个。正如@SusanSwitzer 已经提到的。主要问题是您使用input$land。所以。只需替换为input$country。第二。将colour 映射到country,这是df 中的varname。第三,我切换到 aes_string 而不是 aes 来使用字符输入:

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)

【讨论】:

  • 谢谢!有用! :) 请问为什么要使用 aes_string ?我以前从未见过。
  • 我的荣幸。使用aes_string,您可以将varnames 作为字符串传递给ggplot,这使得input$var 更易于使用,因为它是一个字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 2014-09-29
  • 2016-03-23
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多