简体   繁体   English

警告:错误:美学必须是长度 1 或与数据 (1) 相同:x、组和颜色

[英]Warning: Error in : Aesthetics must be either length 1 or the same as the data (1): x, group and colour

I can get this to work without using Shiny but I am getting this error.我可以在不使用 Shiny 的情况下让它工作,但我收到了这个错误。 Basically I am trying to add lines to the graph based on a checkbox.基本上我正在尝试基于复选框向图形添加线条。 I do this by creating the vector of values using the checkbox input and then filter the dataframe.为此,我使用复选框输入创建值向量,然后过滤 dataframe。 I am only trying to get covidplot to work at the moment.我现在只是想让covidplot工作。 This is a MWE.这是一个 MWE。

UI用户界面

# Define UI for random distribution app ----
ui <- fluidPage(

# App title ----
titlePanel("PM-272 Covid Dashboard"),

# Sidebar layout with input and output definitions ----
sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
        
        # Input: Select the columns ----
        radioButtons("var", "Plot Type:",
                     c("Raw" = "raw",
                       "Rolling Average" = "avg")),
        
        # br() element to introduce extra vertical spacing ----
        br(),
        
        checkboxGroupInput("region", "Regions to show:",
                           c("East Midlands" = "East Midlands",
                             "East of England" = "East of England",
                             "London" = "London",
                             "North East" = "North East",
                             "North West" = "North West",
                             "South East" = "South West",
                             "South West" = "South West",
                             "West Midlands" = "West Midlands",
                             "Yorkshire and The Humber" = "Yorkshire and The Humber"))
        
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
        
        # Output: Tabset w/ plot, summary, and table ----
        tabsetPanel(type = "tabs",
                    tabPanel("Cases", plotOutput("covidplot")),
                    tabPanel("Deaths", plotOutput("deathsplot"))
        )
        
    )
)
)

Server服务器

library(httr)
library(tidyverse)

url <- 'https://api.coronavirus.data.gov.uk/v2/data?areaType=region&metric=covidOccupiedMVBeds&metric=newCasesByPublishDate&metric=newDeaths28DaysByDeathDate&metric=newDeaths60DaysByDeathDate&metric=newCasesByPublishDateRollingRate&format=json'

response <- httr::GET(url,timeout(10))

json_text <- content(response, "text")
df <- jsonlite::fromJSON(json_text)
df<-df$body
df[,c(2,3,4)] <- lapply(df[,c(2,3,4)], as.factor)

# isolate the table 
df$date = as.Date(df$date, "%Y-%m-%d")

df <- df %>% filter(date >= '2020-07-13')

# Define server logic for random distribution app ----
server <- function(input, output) {
    
    # Reactive expression to generate the requested distribution ----
    # This is called whenever the inputs change. The output functions
    # defined below then use the value computed from this expression
    d <- reactive({
        var <- switch(input$var,
                       raw = newCasesByPublishDate,
                       avg = newCasesByPublishDateRollingRate,
                       newCasesByPublishDate)
    })
    
    # Generate a plot of the data ----
    # Also uses the inputs to build the plot label. Note that the
    # dependencies on the inputs and the data reactive expression are
    # both tracked, and all expressions are called in the sequence
    # implied by the dependency graph.
    output$covidplot <- renderPlot({
        input$region
        ycol <- input$var
        grp <- input$region
        
        df2 <- df %>% filter(areaName %in% grp)
        
        covidplot <- df2 %>% ggplot(aes(x=date,y=ycol,group=areaName,color=areaName)) +
            geom_line() +
            geom_point() +
            xlab("Date") +
            ylab("Cases") +
            ggtitle("Covid Cases Over Time By Region")
        ggplotly(covidplot)
    })
    
}

Initially, input$region is null and you have not addressed that situation.最初, input$region是 null 并且您没有解决这种情况。 Try this尝试这个

output$covidplot <- renderPlot({
    if (is.null(input$region)) return(NULL)
    ycol <- input$var
    grp <- input$region
    
    df2 <- df %>% filter(areaName %in% grp)
    
    covidplot <- df2 %>% ggplot(aes(x=date,y=ycol,group=areaName,color=areaName)) +
      geom_line() +
      geom_point() +
      xlab("Date") +
      ylab("Cases") +
      ggtitle("Covid Cases Over Time By Region")
    ggplotly(covidplot)
  })

暂无
暂无

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

相关问题 美学必须是长度1或与数据(1)相同:颜色 - Aesthetics must be either length 1 or the same as the data (1): colour 错误:Aesthetics 必须为长度 1 或与数据 (1) 相同:x - Error: Aesthetics must be either length 1 or the same as the data (1): x R:错误:美学必须是长度1或与数据相同:x - R: Error: Aesthetics must be either length 1 or the same as the data: x 错误:美学长度必须为 1 或与数据 (2190) 相同:x - Error: Aesthetics must be either length 1 or the same as the data (2190): x Shiny 应用程序错误:美学必须是长度 1 或与数据 (1) 相同:x - Shiny App Error: Aesthetics must be either length 1 or the same as the data (1): x 错误:美学必须是长度为1或与数据(1)相同:x和y - Error: Aesthetics must be either length 1 or the same as the data (1): x and y (R) 错误:美学必须是长度为 1 或与数据 (6) 相同:x 和 y - (R) Error: Aesthetics must be either length 1 or the same as the data (6): x and y GGPlot2:错误:Aesthetics 必须是长度为 1 或与数据相同 (16):x, y, group - GGPlot2: Error: Aesthetics must be either length 1 or the same as the data (16): x, y, group ggplot:错误:美学必须为长度1或与数据(10)相同:x,y,group - ggplot: Error: Aesthetics must be either length 1 or the same as the data (10): x, y, group 错误:美学的长度必须为1或与数据相同(4) - Error: Aesthetics must be either length 1 or the same as the data (4)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM