gpt4 book ai didi

r - Shiny :将 input$var 传递给 ggplot2 中的 aes()

转载 作者:行者123 更新时间:2023-12-04 08:23:03 24 4
gpt4 key购买 nike

我正在尝试编写一个 Shiny 应用程序,按年龄 (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))

编辑:正如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(input$Group_select) 将正确呈现图形!

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

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

这是完整的 Shiny 代码:

用户界面
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")
)
))

服务器
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))
})

最佳答案

我不确定这仍然是一个悬而未决的问题,但可以替代 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()


# 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



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

关于r - Shiny :将 input$var 传递给 ggplot2 中的 aes(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com