Brillante: pasar la entrada $ var a aes () en ggplot2
Estoy tratando de escribir una aplicación Shiny que grafica la densidad de una variable VAL, por categorías de edad (EDAD) o sexo (SEXO). El usuario selecciona "SEXO" o "EDAD" en el menú desplegable, y he estado tratando de usarfill = input$Group_select
en ggplot y ggvis.
En ggplot:
output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) +
aes(x= VAL, fill=input$Group_select) +
geom_density(adjust=input$slider1))
EDITAR: como señaló docendo, esto se puede solucionar para ggplot usando 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))):
En 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")
SOLUCIÓN: el uso de as.name (input $ Group_select) representará el gráfico correctamente.
Esto es (era) lo que se representa:Enlace Imgur a salida brillante. Curiosamente, parece que group_by_ interpreta correctamente input $ Group_select, mientras que input $ Group_select se trata como una constante enfill=input$Group_select
¿Alguna idea sobre cómo podría hacer que las parcelas se procesen correctamente?
Aquí está el código completo de Shiny:
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")
)
))
servidor.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))
})