散点图的相关系数

Correlation coefficient for a Scatter plot

我已经为每个国家/地区绘制了散点图,并且我试图在散点图下添加相关系数,但我一直收到错误提示“选择不能有缺失值”。即使在使用 na.rm

之后

有人可以帮我解决这个问题吗?? 感谢您提供的任何帮助。

数据linkEuropeIndia

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(plotly)
library(DT)
library(tidyverse)
library(car)
library(ggpubr)
covid <- read.csv("EuropeIndia.csv")


title <- tags$a(href='https://ourworldindata.org/covid-vaccinations?country=OWID_WRL',
                'COVID 19 Vaccinations')

# Define UI for application 
ui <- fluidPage(
  headerPanel(title = title),
  
  # Application title
  titlePanel("COVID vaccinations: Deaths Vs All variables"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("location", "1. Select a country",
                  choices = covid$location, selectize = TRUE, multiple = FALSE),
      br(),
      helpText("2. Select variables for scatterplot"),
      selectInput(inputId = "y", label = "Y-axis:",
                  choices = c("total_deaths", "new_deaths"), 
                  selected = "Deaths",),
      br(),
      selectInput(inputId = "x", label = "X-axis:",
                  choices = names(subset(covid,select = -c(total_deaths,new_deaths,
                                                           iso_code, continent,date,location), na.rm =TRUE)),
                  selectize = TRUE,
                  selected = "Comparator variables")
    ),
    mainPanel(
      textOutput("location"),
      #plotOutput("Scatterplot"),
      tabsetPanel(
        type = "tabs",
        tabPanel("Scatterplot", plotlyOutput("scatterplot"),
                 verbatimTextOutput("correlation"),
                 verbatimTextOutput("interpretation")),
        tabPanel("Summary of COVID data", verbatimTextOutput("summary")),
        tabPanel("Dataset", DTOutput("dataset")))
    )
  )
)

# Define server logic 
server <- function(input, output) {
  output$location <- renderPrint({locationfilter <- subset(covid, covid$location == input$location)})
  output$summary <- renderPrint({summary(covid)})
  output$dataset <- renderDT(
    covid, options = list(
      pageLength = 50,
      initComplete = JS('function(setting, json) { alert("done"); }')
    )
  )
  
  output$scatterplot <- renderPlotly({
    ggplotly(
      ggplot(subset(covid, covid$location == input$location),
             aes(y = .data[[input$y]], x = .data[[input$x]],col = factor(stringency_index)))+
        geom_smooth()+geom_point()+labs(col ="Stringency Index") 
                )
  })
  
  output$correlation <- renderText({
    x= subset(covid, covid$location == input$location) %>% dplyr::select(as.numeric(!!!input$x, na.rm =TRUE))
    y= subset(covid, covid$location == input$location) %>% dplyr::select(as.numeric(!!!input$y, na.rm = TRUE))
    var(x,y, na.rm = T, use)
    cor(x,y, method = 'pearson', na.rm =T)
    })
}


# Run the application 
shinyApp(ui = ui, server = server)

首先你应该 select select离子列表中的一个国家。

为了进行错误检查,我建议您使用下一个代码。

library(shiny)
library(plotly)
library(DT)
library(tidyverse)
library(car)
library(ggpubr)
covid <- read.csv("EuropeIndia.csv")


title <- tags$a(href='https://ourworldindata.org/covid-vaccinations?country=OWID_WRL',
            'COVID 19 Vaccinations')

# Define UI for application 
ui <- fluidPage(
headerPanel(title = title),

# Application title
titlePanel("COVID vaccinations: Deaths Vs All variables"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
sidebarPanel(
  selectInput("location", "1. Select a country",
              choices = covid$location[1], selectize = TRUE, multiple = FALSE),
  br(),
  helpText("2. Select variables for scatterplot"),
  selectInput(inputId = "y", label = "Y-axis:",
              choices = c("total_deaths", "new_deaths"), 
              selected = "Deaths",),
  br(),
  selectInput(inputId = "x", label = "X-axis:",
              choices = names(subset(covid,select = -c(total_deaths,new_deaths,
                                                       iso_code, continent,date,location), na.rm =TRUE)),
              selectize = TRUE,
              selected = "Comparator variables")
),
mainPanel(
  textOutput("location"),
  #plotOutput("Scatterplot"),
  tabsetPanel(
    type = "tabs",
    tabPanel("Scatterplot", plotlyOutput("scatterplot"),
             verbatimTextOutput("correlation"),
             verbatimTextOutput("interpretation")),
    tabPanel("Summary of COVID data", verbatimTextOutput("summary")),
    tabPanel("Dataset", DTOutput("dataset")))
)
)
)

# Define server logic 
server <- function(input, output) {
output$location <- renderPrint({locationfilter <- subset(covid, covid$location == input$location)})
output$summary <- renderPrint({summary(covid)})
output$dataset <- renderDT(
covid, options = list(
  pageLength = 50,
  initComplete = JS('function(setting, json) { alert("done"); }')
 ) 
)

output$scatterplot <- renderPlotly({
ggplotly(
  ggplot(subset(covid, covid$location == input$location),
         aes(y = .data[[input$y]], x = .data[[input$x]],col = factor(stringency_index)))+
    geom_smooth()+geom_point()+labs(col ="Stringency Index") 
  )
})

output$correlation <- renderText({
x <- covid[covid$location == input$location, input$x]
y <- covid[covid$location == input$location, input$y]
xy = data.frame(x,y)
xy = xy[complete.cases(xy),]
var(xy)
cor(xy,method = 'pearson')
})
}


# Run the application 
shinyApp(ui = ui, server = server)