gpt4 book ai didi

r - 带有多行的 Shiny 和 ggplot2

转载 作者:行者123 更新时间:2023-12-04 09:38:04 26 4
gpt4 key购买 nike

我是 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)

最佳答案

尝试这个。正如@SusanSwitzer 已经提到的那样。主要问题是您使用 input$land .所以。只需替换为 input$country .第二。 map colourcountry这是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)

关于r - 带有多行的 Shiny 和 ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62459083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com