[英]Shiny and ggplot2 with multiple lines
我是 Shiny 的新手,在嘗試渲染 ggplot 時遇到了問題。 我想用多行渲染 plot 但我收到錯誤:警告:錯誤:美學必須是長度 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)
嘗試這個。 正如@SusanSwitzer 已經提到的那樣。 主要問題是您使用input$land
。 所以。 只需替換為input$country
。 第二。 Map country
/地區的colour
,它是 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.