【问题标题】:Displaying a variable amount of lines in a ggplot shiny app在 ggplot 闪亮的应用程序中显示可变数量的行
【发布时间】:2016-10-15 16:35:45
【问题描述】:

hola我遇到了一些问题。我正在创建一个 ggplot2 Shiny 应用程序,用户可以在其中选择折线图中显示的变量数量。我使用了ActionInput的参数倍数,尽管选择了多个变量时,应用程序崩溃,但收到错误:

Warning: Error in .subset2: subscript out of bounds

这里是data

服务器:

  library(shiny) 
  library(googlesheets)
  library(reshape2)
  library(ggplot2)
  library(scales)
  #google authorization, token storage, file acquiztion and assignment


  myData_off <- melt(ridership_hour_off, id.vars = "time")  
  dat_off <- myData_off[myData_off$time != "Total",]
  dat_off$time_ <- as.POSIXct(paste(dat_off$time), origin = "7:00 AM", format = "%I:%M %p", tz = "UTC")
  dcast_off <- dcast(dat_off, time_~variable)

  shinyServer(function(input, output, session) {
    observe({
      monthBy_off <- input$month
      trick_off <- dcast_off[[monthBy_off]]
    output$plot_off <- renderPlot({
           O <-ggplot(data = data.frame( x = dcast_off$time_, y = trick_off), aes(x=x,y=y)) +

           geom_line(size = 2, alpha = 0.75) +
           geom_point(size =3, alpha = 0.75) +

          ggtitle("Boarding the Bus Ridership December 2015") +
          labs(x="Time",y="Count")+
          theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0.5)) +
          theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22))+
         theme_classic()
        O
        })

      })

    })

用户界面:

  library(shiny)
  vars <- list("December" = "dec",
            "January" = "jan",
            "February" = "feb",
            "March" = "mar",
            "April" = "apr",
            "May" = "may",
            "June" = "jun",
            "July" = "jul",
            "August" = "aug",
            "September" = "sep",
            "October" = "oct",
            "November" = "nov")
  shinyUI(fluidPage(
    headerPanel("Cross Acton Transit"),
    titlePanel("Data Dashboard"),
    # Your input selection
    sidebarPanel(
       selectInput("month", "Select plot", vars, selected = "apr", multiple = TRUE)
    ),
    # Show the selected plot
    mainPanel(
      tabsetPanel(
        tabPanel("Plot",
                 plotOutput("plot_off")
        )
      )
    )
  ))

感谢您的意见

【问题讨论】:

  • 根据我的研究,我认为也许我需要一个反应式表达而不是观察。换掉它们没有用。
  • 你想要一个每月有不同线条的情节吗?
  • 是的,我想选择哪些线条在图表上,哪些不在图表上。

标签: r ggplot2 shiny


【解决方案1】:

错误来自dcast_off[[monthBy_off]],它只适用于一个单一的值,而input$month 包含一个字符向量,因为multiple=TRUE 允许选择超过一个月。

您必须使用以下语法:trick_off &lt;- dcast_off[,monthBy_off] 来消除错误。

但是您必须修复绘图错误,例如通过以不同颜色绘制每个月。为此,您最好使用长 data.frame(dcast() 之前):

server <- shinyServer(function(input, output, session) {
        observe({

                trick_off <- dat_off[dat_off$variable %in% input$month,]
                output$plot_off <- renderPlot({
                        O <-ggplot(data = trick_off, aes(x=time_,y=value, color=variable)) +

                                geom_line(size = 2, alpha = 0.75) +
                                geom_point(size =3, alpha = 0.75) +

                                ggtitle("Boarding the Bus Ridership December 2015") +
                                labs(x="Time",y="Count")+
                                theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0.5)) +
                                theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22))+
                                theme_classic()
                        O
                })

        })

})

【讨论】:

  • 顺便说一句,这太棒了。因此,input$month 从 UI 获取输入并选择 dat_off$variable 中所有与输入对应的行。如果它有列,它就是一个数据框,所以data = trick_off 可以工作,你可以在那里分配 x、y 和颜色列吗?那么,如果不是通过 group 参数,不同的月份是如何分开和存储的呢?抱歉所有问题,我只是想了解其中一些是如何工作的/
  • 当你写 color=variable 你告诉ggplot() 有几个系列。
猜你喜欢
  • 2020-12-08
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 2013-07-08
  • 2019-05-01
相关资源
最近更新 更多