gabrown86
gabrown86

Reputation: 1661

Plotting a geom_rect with aes using local variables

I am trying to run shiny and within one of the renderPlot I define a data frame using ddply, and because I use one of the input variables of the function, I need to use the "here" function :

    Dates<-ddply(rawData.Agg,.(ProdID,LD_TSq),here(summarise),
             FOD=min(Time_Seq),
             FOD_ind=which.min(Time_Seq),
             ...
    )

Later in the function I use these values to plot some ggplots

print(  ggplot(rawData.Agg,aes(Time_Seq,Stores)) + 
  geom_vline(xintercept=as.numeric(as.character(Dates$LD_TSq[1])), linetype="solid",colour="cornflowerblue", size=2 ) +
 ...
)

And this works and accesses the Dates dataframe fine. However I would like to add a geom_rect and I do it using:

  geom_rect(aes(xmin=Dates$date_5[1], xmax=Dates$date_3[1], ymin=0, ymax=Inf),fill = "aquamarine",alpha=0.01)

And I get the error in the rstudio terminal of:

Listening on port 8100
Error in eval(expr, envir, enclos) : object 'Dates' not found    

I think this is due to the Dates being a local data frame. I have tried playing around with defining the environment, but cannot get it to work, can anyone show me how to get this working?

EDIT: Here is some code which should replicate the problem using the mtcars dataset.

server.R:

library(shiny)
library(datasets)
library(ggplot2)
library(plyr)
mpgData <- mtcars
shinyServer(function(input, output) {
  output$detailed <- renderPlot({
    Dates<-ddply(mpgData,.(cyl),summarise,
                 disp = ave(disp),
                 hp = ave(hp),
                 wt = ave(wt)
    )
    print(  ggplot(mpgData,aes(disp,hp))+
            geom_point(shape=17,color="black",size=2) +
            geom_rect(aes(xmin=Dates$disp[1], xmax=200, ymin=0, ymax=Inf),fill = "aquamarine",alpha=0.01) 
    )
  })
})

ui.R:

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Launch Date"),

  sidebarPanel(    


  ),

  mainPanel(
    h3(textOutput("caption")),
    tabsetPanel(
      tabPanel("Detailed",plotOutput("detailed"))
    )
  )
))

Upvotes: 1

Views: 1523

Answers (1)

juba
juba

Reputation: 49033

You should remove the call to aes in your geom_rect because Dates$disp[1] is a constant here :

print(  ggplot(mpgData,aes(disp,hp))+
        geom_point(shape=17,color="black",size=2) +
        geom_rect(xmin=Dates$disp[1], xmax=200, ymin=0, ymax=Inf,fill = "aquamarine",alpha=0.01) 
)

Upvotes: 2

Related Questions