我正在尝试根据年龄(AGE)或性别(SEX)类别编写一个闪亮的应用程序来绘制变量VAL的密度.用户在下拉菜单中选择"SEX"或"AGE",我一直试图fill = input$Group_select在ggplot和ggvis中使用.
在ggplot中:
output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) +
aes(x= VAL, fill=input$Group_select) +
geom_density(adjust=input$slider1))
Run Code Online (Sandbox Code Playgroud)
编辑:正如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))):
Run Code Online (Sandbox Code Playgroud)
在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")
Run Code Online (Sandbox Code Playgroud)
解决方案:使用as.name(输入$ Group_select)将正确呈现图形!
这是呈现的内容:Imgur链接到闪亮的输出.有趣的是,似乎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")
)
))
Run Code Online (Sandbox Code Playgroud)
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))
})
Run Code Online (Sandbox Code Playgroud)
SOw*_*wla 10
我不确定这仍然是一个悬而未决的问题,但替代方法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()
Run Code Online (Sandbox Code Playgroud)

# 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()
Run Code Online (Sandbox Code Playgroud)

# 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
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.0)于 2018 年 11 月 5 日创建。