2

I am writing a shiny app and using ggplot to plot a density map. Unfortunately, I am not able to get ggplot to understand my input variable in the aes() section. Below is a part of the code that is problematic in my server.R:

  output$mapPlot <- renderPlot({
    ggplot(data=Robbery,aes(x=X.Coordinate.Code,y=Y.Coordinate.Code),environment = .e) +  
     stat_density2d(aes(fill=..density..,alpha=cut(..density..,breaks=c(0,input$ranged,Inf))),
                     geom = "raster", contour = FALSE) 
 })

The error output is : Error in cut.default: object 'input' not found

The input variable that I want to pass is input$ranged. I have seen the suggestion of using aes_string() instead of aes() when using a variable like this. My problem is that when I use aes_string() it is not able to understand what ..density.. is. Is there another way to get ggplot to accept the input$ranged variable other then using aes_string()?

Thank you very much.

Note: when I just print out the input$ranged variable in my app, it works fine. Here is the server.R code for just printing input$ranged:

output$mapText = renderText({
    paste("the cutoff is",input$ranged)
  })

Here is a little R code I've added that can be run to reproduce the problem:

library(ggplot2)

    set.seed(42)
    df =  data.frame(matrix(rnorm(2000), nrow=1000))
    df
    ggplot(data= df,aes(x=X1,y=X2)) + geom_point() +
      stat_density2d(aes(fill=..density..,alpha=cut(..density..,breaks=c(0,0.02,Inf))),geom = "raster", contour = FALSE)

    test=0.02

    ggplot(data= df,aes(x=X1,y=X2)) + geom_point() +
      stat_density2d(aes(fill=..density..,alpha=cut(..density..,breaks=c(0,test,Inf))),geom = "raster", contour = FALSE)

The "test" variable is not understood by ggplot. But, if I use aes_string() instead of aes(), the ..density.. is not understood.

DTM
  • 23
  • 3
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. This probably isn't specific to shiny. But it's unclear even what value `input$ranged` contains. – MrFlick Jan 30 '18 at 18:41
  • Hi MrFlick, Note taken. Sorry, it's my first time posting. Here is a simple R code (No Shiny) where the problem is reproduced. So in this case, when I try to pass the "test" variable to ggplot, it's not able to handle it. – DTM Jan 30 '18 at 19:17

1 Answers1

0

So there's one other member of the aes() family, called aes_q(). Because you need to build an expression here, I think the would be the easiest to use. For example

test <- 0.02
ggplot(data=df, aes(x=X1,y=X2)) + geom_point() + 
  stat_density2d(aes_q(
      fill = quote(..density..), 
      alpha = bquote(cut(..density..,breaks=c(0, .(test), Inf)))
      ), geom = "raster", contour = FALSE)

Here we use bquote to "insert" the numeric value into the breaks expression. If we look at just that chunk we see we get the expression we want

bquote(cut(..density..,breaks=c(0,.(test),Inf)))
# cut(..density.., breaks = c(0, 0.02, Inf))

Depending on how you defined your Shiny input, you'll also want to make sure it's numeric (many are character values by default). So you may need to use as.numeric(input$ranged)

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This worked perfectly. In the example as well as in my shiny app. Thank you MrFlick! – DTM Jan 30 '18 at 20:03