简体   繁体   English

R Shiny Aesthetics 必须是长度为 1 或与数据 (8) 相同: y

[英]R shiny Aesthetics must be either length 1 or the same as the data (8): y

I am trying to plot multiple line charts in R Shiny and include checkboxes but I get an error message when I try to select more than one:我试图在 R Shiny 中绘制多个折线图并包含复选框,但是当我尝试选择多个折线图时收到一条错误消息:

Error: Aesthetics must be either length 1 or the same as the data (8): y错误:Aesthetics 必须为长度 1 或与数据 (8) 相同:y

I was able to plot them without error when using radio buttons, but checkboxes don't work.使用单选按钮时,我能够毫无错误地绘制它们,但复选框不起作用。 Here's my code:这是我的代码:

library(readxl)
scores <- read.csv("newscores.csv")
weeks <- read.csv("weeks.csv")

library(ggplot2)

# ui.R
fluidPage(    
  # Give the page a title
  titlePanel("Fantasy Scores"),

  # Generate a row with a sidebar
  sidebarLayout(   
    # Define the sidebar with one input
    sidebarPanel(
      checkboxGroupInput("Team", label = h3("Select Team"), 
                         choices = c("Jeff","Jordan","Emmerts",
                                     "CJ","Jimmy","Phil",
                                     "Mat","Clegg","Rob","Shawn",
                                     "Seth","Truscott"))
    ),

    # Create a spot for the barplot
    mainPanel(
      plotOutput("phonePlot")  
    ) 
  )
)

# server.R
function(input, output) {

  # Fill in the spot we created for a plot
  output$phonePlot <- renderPlot({

    # Render a barplot
    ggplot(weeks, aes(x = Week, y = weeks[,input$Team])) + 
      geom_point() + geom_line()+ylim(59,160)+labs(title="Scores by Week",
                                                   x="Week",y="Points")+theme_minimal()
  })
}

And I pasted the dataset here (it's quite small) ( weeks.csv ):我在这里粘贴了数据集(它非常小)( weeks.csv ):

structure(list(Week = c(1, 2, 3, 4, 5, 6, 7, 8), Jeff = c(150.4, 
91.5, 85.9, 122.9, 84.8, 116.6, 107.7, 101.3), Jordan = c(111, 
89.7, 127.6, 105.1, 115.1, 84, 108.9, 65.6), Emmerts = c(128.9, 
108.6, 92.7, 96.4, 78, 73.8, 131, 120.4), CJ = c(72.8, 104.1, 
84, 92.9, 62.8, 59.4, 82.2, 70.7), Jimmy = c(76.7, 68.4, 105.7, 
111.3, 97.3, 108.5, 102.6, 109.7), Phil = c(96.2, 83.9, 91.5, 
98.8, 81.7, 71.7, 78.3, 76.6), Mat = c(93.6, 106.2, 92.8, 87.5, 
131, 152.7, 142.6, 105.1), Clegg = c(117.9, 93.7, 98, 109.8, 
95.5, 104.2, 93.3, 103.1), Rob = c(80, 72.1, 74, 84.8, 111.7, 
105, 89.4, 77.6), Shawn = c(84.4, 116.6, 80.9, 106.1, 106.2, 
85.5, 88.4, 107.2), Seth = c(131.9, 98.6, 87.2, 111.7, 109, 125.7, 
96.3, 108.9), Truscott = c(100.5, 68.5, 88.8, 96.3, 91.5, 97.6, 
70.4, 111.3)), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8"))

Thanks for the help!谢谢您的帮助!

This is a solution:这是一个解决方案:

library(reshape2)
library(dplyr)
library(ggplot2)

#input <- NULL
#input$Team <- c("Seth", "Truscott")

weeks_melt <- melt(weeks, "Week") 
p <- ggplot(weeks_melt, aes(x = Week, y = value, color = variable)) + geom_blank()

weeks_filtered <- week_melt %>% filter(variable %in% input$Team)
p + geom_line(data = weeks_filtered)

Note that we create a geom_blank() so ggplot creates the scales right away (so the colors are fixed even when you change what is selected), then we filter the data before adding the lines.请注意,我们创建了一个geom_blank()以便 ggplot 立即创建比例(因此即使您更改所选内容,颜色也是固定的),然后我们在添加线条之前过滤数据。 If we don't fix the scales, all the colors and axis scales will vary each time you include or remove one of the factor levels, and this is not a good idea in most cases.如果我们不固定比例,每次包含或删除一个因子水平时,所有颜色和轴比例都会有所不同,这在大多数情况下不是一个好主意。

Shiny doesn't require you to build your plots in a single line, so you're free to filter your data before doing anything. Shiny不需要您在一行中构建绘图,因此您可以在做任何事情之前自由地过滤数据。 Another point is that the first two non-commented lines could be outside your server function, to save a bit of time when transitioning between visualizations.另一点是前两行未注释的行可能在您的服务器功能之外,以在可视化之间转换时节省一些时间。

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

相关问题 闪亮的单选按钮-美学长度必须为1或与数据相同 - Shiny radio buttons - Aesthetics must be either length 1 or the same as the data 如何解决Shiny中的“美学长度必须等于1或与数据相同” - How to solve “Aesthetics must be either length 1 or the same as the data” in Shiny Shiny 应用程序错误:美学必须是长度 1 或与数据 (1) 相同:x - Shiny App Error: Aesthetics must be either length 1 or the same as the data (1): x 美学的长度必须为1或与数据(10)相同:x,填写r闪亮 - Aesthetics must be either length 1 or the same as the data (10): x, fill in r shiny 美学必须是长度1或与数据(207)相同:x,y - Aesthetics must be either length 1 or the same as the data (207): x, y 错误:美学必须是长度 1 或与数据 (5) 相同:y - Error: Aesthetics must be either length 1 or the same as the data (5): y 美学必须是长度 1 或与数据相同 (366):x, y - Aesthetics must be either length 1 or the same as the data (366): x, y 美学的长度必须为1或与数据相同:ymax,ymin,x,y - Aesthetics must be either length 1 or the same as the data: ymax, ymin, x, y 错误:美学必须是长度为1或与数据(1)相同:x和y - Error: Aesthetics must be either length 1 or the same as the data (1): x and y 美学的长度必须为1或与数据(1)相同:x,y,标签 - Aesthetics must be either length 1 or the same as the data (1): x, y, label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM