闪亮的+ ggplot:如何对响应式数据对象进行子集处理?

5
我正在制作一个shiny应用程序,在用户选择他感兴趣的日期范围(即x轴的范围)之后,它会显示一个ggplot。因此,我想我需要定义一个反应式数据对象(正确吗?)。
该ggplot中有一些子集。R告诉我,“反应式数据对象不可子集化”。在我对ggplot的新手理解中,必须在geom_bar()、geom_line()语句内完成子集操作才能得到所需的图形。
  1. Can anyone suggest me how to proceed with the subsetting?
  2. And how to reference the factor category in generating colors for the graph? Thanks!

    sample data

    A = c(3, 4, 3, 5)
    B = c(2, 2, 1, 4)
    Z = c(1, 2, 1, 2)
    R = c(-2, -1, -3, 0)
    S = c(7,7,7,9)
    mydata = data.frame(cbind(A,B,Z,R,S))
    dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
    mydata$date = as.Date(dates)
    mydata.m = melt(mydata,id="date")
    names(mydata.m) = c("variable", "category","value")
    

    shiny server: select observations as per user input (dateRangeInput)

    data.r = reactive({
      a = subset(mydata.m, variable %in% input$daterange)
      return(a)
    })
    

    shiny server: make the plot

    output$myplot = renderPlot({
    
      # ggplot with proper reference to reactive function <<data.r()>>
      s = ggplot(data.r(), aes(x=variable, fill=category)) +   
    
        # bars for categories A, B, Z: needs subsetting the data... but how?
        + geom_bar(data=subset(data.r(), category %in% c("A","B")), aes(y=value), stat="identity", position="stack")
        + geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity")
    
        # lines for categories R, S: same.
        + geom_line(subset=.(category=="R"), aes(y=value)) 
        + geom_line(subset=.(category=="S"), aes(y=value))
    
        # how to reference the factor <<category>> in reactive function <<data.r()>>?
        + scale_fill_manual(breaks = levels(category), values = mycolorgenerator(length(levels(category))))
    
      print(s)
    
    })
    

UI.R

# INPUT PART

library(shiny)

shinyUI(pageWithSidebar(
  # Application title
  headerPanel("My App"),

  sidebarPanel( 

    dateRangeInput("daterange", "Date range:",
               start  = "2014-01-01",
               end    = "2014-04-01",
               min    = "2014-01-01",
               max    = "2014-04-01",
               format = "dd/mm/yyyy",
               separator = "-"),

    submitButton(text="Update!")
  ),
# -----------------------------------------------

# OUTPUT PART

  mainPanel(
    tabsetPanel(
      tabPanel("Tab 1", h4("Head 1"),plotOutput("myplot"))
    )
  )
))

SERVER.R

library(reshape)
library(shiny)
library(ggplot2)



# GEN DATA -----------------------------------------------

A = c(3, 4, 3, 5)
B = c(2, 2, 1, 4)
Z = c(1, 2, 1, 2)
R = c(-2, -1, -3, 0)
S = c(7,7,7,9)
mydata = data.frame(cbind(A,B,Z,R,S))
dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
mydata$date = as.Date(dates)
mydata.m = melt(mydata,id="date")
names(mydata.m) = c("variable", "category","value")







# SERVER -----------------------------------------------
shinyServer(function (input, output) {


# DATA

data.r = reactive({
  a = subset(mydata.m, variable %in% input$daterange)
  return(a)
})



# GGPLOT

mycolorgenerator = colorRampPalette(c('sienna','light grey')) 


output$myplot = renderPlot({

  # ggplot with proper reference to reactive function <<data.r()>>
  s = ggplot(data.r(), aes(x=variable, fill=category))  +  

    # bars for categories A, B, Z: needs subsetting the data... but how?
     geom_bar(data=subset(data.r(), category %in% c("A","B")), aes(y=value), stat="identity", position="stack") +
     geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity") +

    # lines for categories R, S: same.
     geom_line(subset=.(category=="R"), aes(y=value)) +
     geom_line(subset=.(category=="S"), aes(y=value)) +

    # how to reference the factor <<category>> in reactive function <<data.r()>>?
     scale_fill_manual(breaks = levels(category), values = mycolorgenerator(length(levels(category))))

  print(s)


})
})

1
请问您能否提供您的ui.R代码和完整的server.R文件以便测试此应用程序? - nrussell
@nrussell:我理解你的问题,但实际程序要复杂得多,我花了一个小时从中提取出我认为与我的问题相关的内容,并将其转化为一个简单易懂的示例(但完全是无意义的)。如果您能直接基于代码给出一些建议,我将非常感激。顺便说一下,您知道ggplot部分,几天前您回答了一个关于它的问题 :),但这里没有实现,因为与此问题无关(我想)。 - user3817704
1
没错,但是特别是对于 shiny 应用程序来说,如果不能运行应用程序,很难确定问题所在。 - nrussell
好的,我会尽快完成(=明天)。谢谢! - user3817704
.()” 函数应该从哪里来?你是否忘记导入库了? - MrFlick
显示剩余2条评论
1个回答

7

(完整的server.R和ui.R确实非常有帮助)

我不确定你从哪里获取了.()函数或者geom_bar有一个subset=参数的想法。但是这里提供了一个更新的renderPlot,至少没有生成任何错误

output$myplot = renderPlot({

  dd<-data.r()
  # ggplot with proper reference to reactive function <<data.r()>>
  s = ggplot(dd, aes(x=variable, fill=category))  +  

    # bars for categories A, B, Z: needs subsetting the data... but how?
     geom_bar(data=subset(dd, category %in% c("A","B")), aes(y=value), 
         stat="identity", position="stack") +
     geom_bar(data=subset(dd, category=="Z"), aes(y=-value), stat="identity") +

    # lines for categories R, S: same.
     geom_line(data=subset(dd, category=="R"), aes(y=value)) +
     geom_line(data=subset(dd, category=="S"), aes(y=value)) +

     scale_fill_manual(breaks = levels(dd$category), 
         values = mycolorgenerator(length(levels(dd$category))))

  print(s)
})

大部分情况下,我将data=更改为显式的subset()调用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接