闪亮:将输入$ var传递给ggplot2

时间:2016-02-11 17:13:00

标签: r shiny

我正在尝试根据年龄(AGE)或性别(SEX)类别编写一个闪亮的应用程序来绘制变量VAL的密度。用户选择" SEX"或"年龄"在下拉菜单中,我一直在尝试在ggplot和ggvis中使用fill = input$Group_select

在ggplot中:

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

编辑:正如docendo指出的那样,可以使用aes_string修复ggplot:

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

在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")

解决方案:使用as.name(输入$ Group_select)将正确呈现图表!

这是呈现的内容:Imgur link to shiny output。有趣的是,似乎group_by_正确地解释了输入$ Group_select,而输入$ Group_select被视为fill=input$Group_select中的常量

关于如何正确渲染图表的任何想法?

以下是完整的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")
  )
))

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))
})

1 个答案:

答案 0 :(得分:3)

我不确定这仍然是一个悬而未决的问题,但是aes_string的替代方法是使用整洁的评估:

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()

# with tidy eval, you can put the 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

reprex package(v0.2.0)于2018-11-05创建。

相关问题