如何在闪亮中使用 ggplot2 绘制数据框的子集

Posted

技术标签:

【中文标题】如何在闪亮中使用 ggplot2 绘制数据框的子集【英文标题】:How to plot a subset of a dataframe using ggplot2 in shiny 【发布时间】:2021-12-18 14:14:27 【问题描述】:

https://docs.google.com/spreadsheets/d/1mOBYkqqtoltX4kLC1zSyP3S34OK6NN-mCu_rUQydYjM/edit?usp=sharing - EuropeIndia 数据

我的数据集在一个名为 location 的变量中有 45 个不同的国家我试图将数据子集到每个国家并为每个国家绘制散点图 当我运行应用程序时,图表没有出现,我收到 2 个警告

警告1:select输入“位置”包含大量选项;考虑使用服务器端选择来大幅提高性能。请参阅 ?selectizeInput 帮助主题的详细信息部分。 警告 2:“cat 中的错误:参数 1(类型 'list')不能由 'cat' 处理 102:猫” 这是我使用的代码,请让我知道我哪里出错了,提前谢谢

# 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/
#
#
# 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(tidyverse)
covid <- read.csv("D:/R/Practice 3/EuropeIndia.csv")

# Define UI for application 
ui <- fluidPage(
  
  # Application title
  titlePanel("Deaths vs all variables "),
  
  # Sidebar 
  sidebarLayout(
    sidebarPanel(
      selectInput("location", "Select a country",
                  choices = covid$location), selected = NULL),
    mainPanel(
      textOutput("location"),
      tabsetPanel(
        type = "tabs",
        tabPanel("cases",plotOutput("plot1t"), plotOutput("plot1n")),
        tabPanel("vaccinations",plotOutput("plot2t"), plotOutput("plot2n")),
        tabPanel("people",plotOutput("plot3t"),plotOutput("plot3n")),
        tabPanel("full vaccination",plotOutput("plot4t"), plotOutput("plot4n")),
        tabPanel("boosters",plotOutput("plot5t"), plotOutput("plot5n")),
        tabPanel("new vaccination",plotOutput("plot6t"),plotOutput("plot6n")),
        tabPanel("median age",plotOutput("plot7t"),plotOutput("plot7n")),
        tabPanel("new cases",plotOutput("plot8t"), plotOutput("plot8n")),
        tabPanel("summary", verbatimTextOutput("summary")),
        tabPanel("dataset", tableOutput("dataset"))
      )
    )
  )
)

server <- function(input, output) 
  output$location <- renderText(
    locationfilter <- subset(covid, covid$location == input$location)
  )
  output$plot1t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=total_cases))+geom_point()
    
  )
  output$plot1n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=total_cases))+geom_point()
    
  )
  output$plot2t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=total_vaccinations,color =location))+geom_point()
    
  )
  output$plot2n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=total_vaccinations))+geom_point()
    
  )
  output$plot3t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=people_vaccinated))+geom_point()
    
  )
  output$plot3n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=people_vaccinated))+geom_point()
    
  )
  output$plot4t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=people_fully_vaccinated))+geom_point()
    
  )
  output$plot4n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=people_fully_vaccinated))+geom_point()
    
  )
  output$plot5t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=total_boosters))+geom_point()
    
  )
  output$plot5n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=total_boosters))+geom_point()
    
  )
  output$plot6t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=new_vaccinations))+geom_point()
    
  )
  output$plot6n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=new_vaccinations))+geom_point()
    
  )
  output$plot7t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=median_age))+geom_point()
    
  )
  output$plot7n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=median_age))+geom_point()
    
  )
  output$plot8t <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=total_deaths,x=new_cases))+geom_point()
    
  )
  output$plot8n <- renderPlot(
    ggplot(covid[covid$total_cases %in% "A", ],aes(y=new_deaths,x=new_cases))+geom_point()
    
  )
  output$summary <- renderPrint(
    summary(covid)
  )
  output$dataset <- renderTable(
    covid
  )


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

【问题讨论】:

嗨,请提供一个我们可以重现的示例:covid 我会尝试将数据集添加到这个问题中 【参考方案1】:

当使用selectInput() 时,请尝试choices 参数中仅包含唯一选项。此外,我认为您将 selected = NULL 放在了错误的括号中,但我不确定这是否真的会导致任何问题。

sidebarPanel(
  selectInput("location", "Select a country",
              choices = unique(covid$location), selected = NULL))

【讨论】:

哦,我会检​​查一下,但实际问题是我无法生成图表 正如 yuliaUU 在 cmets 中指出的那样,我们无法诊断实际问题,因为您没有提供数据。如果您提供数据,那么我们应该能够提供帮助。 我提供的链接可以用来查看数据

以上是关于如何在闪亮中使用 ggplot2 绘制数据框的子集的主要内容,如果未能解决你的问题,请参考以下文章

闪亮 - 绘制右边框的列

R中数据框的空间子集

R ggplot2:在不同图层上绘制数据子集时保持原始颜色和组级顺序

闪亮的地图上的散点图

如何通过闪亮的 selectInput 动态选择数据帧的子集?

用单独的线型在 ggplot2 中绘制缺失值