Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: passing input$var to aes() in ggplot2

Tags:

r

shiny

I am trying to write a Shiny app graphing the density of a variable VAL, by categories of age (AGE) or sex (SEX). The user selects "SEX" or "AGE" in the dropdown menu, and I have been trying to use fill = input$Group_select in ggplot and ggvis.

In ggplot:

output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) + 
                      aes(x= VAL, fill=input$Group_select) +
                      geom_density(adjust=input$slider1))

EDIT: as docendo pointed out, this can be fixed for ggplot using aes_string:

output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) + 
                              aes(x= VAL) +
                              geom_density(adjust=input$slider1, aes_string(fill=input$Group_select))): 

In ggvis:

gvis <- reactive(subset(gdat, GFR==input$GFR_select) %>% 
                 ggvis(x = ~VAL) %>% group_by_(input$Group_select) %>%
                 layer_densities(adjust = input$slider1) %>%
                 add_axis("y", title = "Density", ticks="none") %>%
                 scale_numeric("x", domain = c(0, 230)) ) 
gvis %>% bind_shiny("ggvis", "ggvis_ui")

SOLUTION: using as.name(input$Group_select) will render the graph properly!

This is (was) what is rendered: Imgur link to shiny output. Interestingly, it seems that group_by_ correctly interprets input$Group_select, whereas input$Group_select is treated as a constant in fill=input$Group_select

Any ideas on how I could get the plots to render correctly?

Here is the full Shiny code:

ui.R

library(shiny)
library(ggplot2)
library(dplyr)
library(ggvis)
shinyUI(fluidPage(
  titlePanel("eGFR in the ARIC study"),
  sidebarPanel(
    selectInput("Group_select",
                label=h3("By-Variable"),
                choices=c("AGE","SEX","ALL"),
                selected="SEX"),
    selectInput("GFR_select",
                label=h3("Creatinine Measure"),
                choices=c("BOTH", "CREATININE", "CYSTATIN", "MDRD"),
                selected="MDRD"),
    sliderInput("slider1", label = h3("Bandwith Adjustment"),
                min = 0.5, max = 4, value = 1)
  ),
  mainPanel(
    uiOutput("ggvis_ui"),
    ggvisOutput("ggvis"),
    plotOutput("plot")
  )
))

server.R

library(shiny)
source("helpers.R")
shinyServer(function(input, output) {
  gvis <- reactive(subset(gdat, GFR==input$GFR_select) %>% 
                     ggvis(x = ~VAL, fill = ~input$Group_select) %>% group_by_(input$Group_select) %>%
                     layer_densities(adjust = input$slider1) %>%
                     add_axis("y", title = "Density", ticks="none") %>%
                     scale_numeric("x", domain = c(0, 230)) ) 
  gvis %>% bind_shiny("ggvis", "ggvis_ui")
  output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) + 
                          aes(x= VAL, fill=input$Group_select) +


      geom_density(adjust=input$slider1))
})
like image 649
Kaskarn Avatar asked Feb 11 '16 17:02

Kaskarn


1 Answers

I'm not sure this is still an open question, but an alternative to the aes_string would be to convert the string to a symbol:

library(ggplot2)

# usually you can put all the shared aesthetics in the first line
ggplot(mtcars, aes(mpg, hp, colour = cyl)) +
  geom_point()

# when the input is a string, you can use aes_string(), but you'd
# have to enter the string var separately, or change all vars..

input <- "cyl"

ggplot(mtcars, aes(mpg, hp)) +
  geom_point(aes_string(colour = input))

# or
# ggplot(mtcars, aes_string("mpg", "hp", colour = input)) +
#  geom_point()

# you can put the converted string var in the normal aes()
ggplot(mtcars, aes(mpg, hp, colour = !!as.symbol(input))) +
  geom_point()

# note: as.name() and as.symbol() are aliases

Created on 2018-11-05 by the reprex package (v0.2.0).

like image 176
SOwla Avatar answered Oct 04 '22 02:10

SOwla