【发布时间】:2021-07-08 09:27:38
【问题描述】:
我正在计算每个国家的correlation,介于每日新冠病例数和每日疫苗接种之间。
有两个 df,一个用于确诊病例,另一个用于疫苗接种:
library(tidyverse)
library(lubridate)
library(glue)
library(scales)
library(tidytext)
library(shiny)
library(shinydashboard)
file_url1 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long4.csv"
file_url2 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/vaccination_data.csv"
ts_all_long <- read.csv(url(file_url1))
vaccination_data <- read.csv(url(file_url2))
ts_all_long <- ts_all_long %>%
mutate(date = as.Date(date))
vaccination_data <- vaccination_data %>%
mutate(date = as.Date(date))
当我使用上述数据在rmarkdown 中运行相关性时,它会起作用:
ts_all_long %>%
left_join(y = vaccination_data,
by = c("Country.Region" = "location", "date", "continent", "iso3c" = "iso_code")) %>%
na.omit() %>%
group_by(Country.Region) %>%
summarise(COR = cor(Confirmed_daily, total_vaccinations),
total_vaccinations_per_hundred = first(total_vaccinations_per_hundred)) %>%
arrange(COR) %>%
na.omit() %>%
slice(c(1:15, ( n()-14): n() ))
问题:当我在shiny 和SelectInput total_vaccinations 中使用它来制作动态参数时,它会给出这个错误:
summarise()输入COR存在问题。 [31mx[39m不兼容 尺寸 [34mi[39m 输入COR是cor(Confirmed_daily, as.numeric(input$id_vaccination_type))。 [34mi[39m 错误 发生在第 2 组:Country.Region = "Argentina"。
用户界面
fluidRow(
style = "border: 1px solid gray;",
h3("Vaccination to Cases Correlation Analysis"),
column(4, style = "border: 1px solid gray;",
selectInput(inputId = "id_vaccination_type", label = "Choose Vaccination Parameter",
choices = c("total_vaccinations","people_vaccinated","people_fully_vaccinated"),
selected = "total_vaccinations")
),
column(8, style = "border: 1px solid gray;",
plotOutput("top_corr_countries", height = "550px") #
)
服务器
corr_data <- reactive({
corr_data <- ts_all_long %>%
left_join(y = vaccination_data,
by = c("Country.Region" = "location", "date", "continent", "iso3c" = "iso_code")) %>%
na.omit() %>%
group_by(Country.Region) %>%
summarise(COR = cor(Confirmed_daily, as.numeric(input$id_vaccination_type)) ,
total_vaccinations_per_hundred = first(total_vaccinations_per_hundred)) %>%
na.omit() %>%
arrange(COR) %>%
na.omit() %>%
slice(c(1:15, ( n()-14): n() ))
})
output$top_corr_countries <- renderPlot({
corr_data() %>%
ggplot(aes(x = COR ,
y = fct_reorder(Country.Region, -COR),
col = COR > 0)) +
geom_point(aes(size = total_vaccinations_per_hundred)) +
geom_errorbarh(height = 0, size = 1, aes(xmin = COR, xmax = 0)) +
geom_vline(xintercept = 0, col = "midnightblue", lty = 2, size = 1) +
theme(
panel.grid.major = element_blank(),
legend.position = "NULL") +
labs(title = glue("Top Countries by +/- Correlation with Vaccination as of {max(vaccination_data$date)}"),
subtitle = "Size is proportional to Vaccination per Population",
y = "", x = "Correlation",
caption = "Data source: covid19.analytics
Created by: ViSa")
})
注意:我也在另一个链接中发布了这个问题,但那里听起来更复杂,所以我试图在这个链接中重新表述它以使其更简单。
【问题讨论】:
标签: r shiny correlation