【问题标题】:Subsetting data in Shiny to create interactive ggplot barchart在 Shiny 中对数据进行子集化以创建交互式 ggplot 条形图
【发布时间】:2016-01-22 13:19:08
【问题描述】:

我正在尝试构建我的第一个闪亮的应用程序,让用户可以探索哥本哈根不同类型的停车违规行为。但是我很难根据用户输入对要绘制的数据进行子集化 - 基本上我正在尝试创建一个交互式版本的下图,用户可以在其中选择 type_1、type_2 等。

#example data
df <- data.frame(c('a', 'b', 'c', 'd', 'e', 'f', 'g'), c(rep_len(55, 5), 0, 44),c(rep_len(12, 5), 6, 6), c(0, 2, 0, 3, 0, 7, 8), c(0, 0, 4, 0, 8, 5, 3), c(5, 0, 6, 0, 11,12, 9))
names(df) <- c("street", "type_1", "type_2", "type_3", "type_4", "type_5")

#Example of static plot that i'm trying to make interactive
ggplot(subset(df, type_5 > 5))+geom_bar(aes(x=reorder(street,type_5), y=type_5),stat = "identity")+coord_flip()+xlab("Streetname")

我的ui.R和server.R如下。我的原始数据有超过 2000 个街道名称,这就是为什么我试图对数据进行子集化以仅包括 type_x > 一些门槛的停车违规行为

# Define the overall UI
ui <- shinyUI(

# Use a fluid Bootstrap layout
fluidPage(    

# Give the page a title
titlePanel("Parking violations"),

# Generate a row with a sidebar
sidebarLayout(      

  # Define the sidebar with one input
  sidebarPanel(
    selectInput("type", "Type of violation:", 
                choices=colnames(df), selected = "type_1" ),
    hr(),
    helpText("Data from Parking Copenhagen")
  ),

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

)
)
)



# Define a server for the Shiny app
server <- shinyServer(function(input, output) {

  #Subset the data based on user input
  p.df <- reactive({
    subset(df, input$type > 5)
  })  


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

    pdat.df <- p.df()

    # Render a barplot
    p <- ggplot(pdat.df, aes(x=pdat.df$street, y=input$type)) + geom_bar(stat = "identity")+coord_flip()

    print(p)

  })
})

shinyApp(ui=ui, server = server)

下面的结果不是我想要的。我已经尝试过shiny not displaying my ggplot as I'd expect. 此处建议的选项,但在ggplot()(如我的静态示例中)或geom_bar() 中的任何一个子集都没有运气

非常感谢任何建议。谢谢!

【问题讨论】:

  • 我无法测试您的代码,但可能与“input$type > 5”和“choices=colnames(df), selected = "type_1"" 有关,type_1 是一个因素吗?因为你过滤了一个数字。
  • type_1-5 是数字,所以我认为这不是问题?
  • 或者在你的响应式()表达式中 df 5); df
  • @MLavoie 不幸的是没有改变任何东西
  • 在作为答案发布之前尝试替换为 p

标签: r ggplot2 shiny


【解决方案1】:

我认为你需要在return( df[df[input$type]&gt;5,]) 上更改subset(df, input$type &gt; 5)

因为输入返回 charactersubset 无法将其解释为 colname 例如(使用您的数据)

subset(df,"type_1">5)
    #  street type_1 type_2 type_3 type_4 type_5
    #1      a     55     12      0      0      5
    #2      b     55     12      2      0      0
    #3      c     55     12      0      4      6
    #4      d     55     12      3      0      0
    #5      e     55     12      0      8     11
    #6      f      0      6      7      5     12
    #7      g     44      6      8      3      9

df[df["type_1"]>5,]
    #  street type_1 type_2 type_3 type_4 type_5
    #1      a     55     12      0      0      5
    #2      b     55     12      2      0      0
    #3      c     55     12      0      4      6
    #4      d     55     12      3      0      0
    #5      e     55     12      0      8     11
    #7      g     44      6      8      3      9

(第 6 行子集)

我在你的闪亮版本中没有看到重新排序 试试

pdat.df <- p.df()
pdat.df$street=reorder(pdat.df$street,pdat.df[[input$type]])

正如@MLavoie 所说的那样

aes_string(x="street", y=input$type)

【讨论】:

    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多