繁体   English   中英

散点图的相关系数 plot

[英]Correlation coefficient for a Scatter plot

我已经为每个国家绘制了散点图,我试图在散点图下添加一个相关系数,但我不断收到错误消息,说“选择不能有缺失值”。 即使在使用 na.rm

有人可以帮我弄这个吗?? 感谢您提供的任何帮助。 在此处输入图像描述

数据链接欧洲印度

 # # 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 从选择列表中选择一个国家。

对于错误检查,我建议您使用下一个代码。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM