【问题标题】:Using shiny input values in a ggplot aes在 ggplot aes 中使用闪亮的输入值
【发布时间】:2020-02-23 08:33:11
【问题描述】:

我正在尝试在 Shiny 中实现这 3 行代码,这是我想要的输出。

install.packages("ggalt")
library(ggalt)

health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph 

ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area))+
    geom_dumbbell()

在 Shiny 中,我希望用户能够选择棒棒糖图的起点和终点。我还希望用户能够选择将显示在 y 轴上的分类变量(在许多分类变量中——尽管目前示例数据只有一个分类变量)。以下是我到目前为止所做的工作,实际上将所有变量替换为输入变量(所以我认为它应该可以工作,但它没有......)

ui<-pageWithSidebar(
  headerPanel('Lollipop Chart'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(health), selected=names(health)[1]),
    selectInput('val1', 'Starting Value', names(health), selected=names(health)[3]), 
    selectInput('val2', 'Ending Value', names(health), selected=names(health)[2])   ),
  mainPanel(
    plotOutput('plot1')
  )
)

server<-function(input, output, session) {

   health < read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph 

  selectedData <- reactive({
    health[, c(input$xcol, input$val1, input$val2)]
  }) 

  output$plot1 <- renderPlot({
ggplot(selectedData(), aes(x=input$val1, xend=input$val2, y=input$xcol))+
    geom_dumbbell()
  })
}

shinyApp(ui, server)

谁能帮助我为什么没有得到想要的输出? :(

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    所需的情节没有出现,因为您以不同的方式调用ggplot。在您闪亮的应用程序中,选定的列名是字符串:

    ggplot(health, aes(x='pct_2013', xend='pct_2014', y='Area'))+
        geom_dumbbell()
    

    aes 替换为aes_string 即可。

    ggplot(health, aes_string(x='pct_2013', xend='pct_2014', y='Area'))+
           geom_dumbbell()
    

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 2019-07-04
      • 2020-03-28
      • 2013-11-01
      • 2019-05-23
      • 2020-06-03
      • 2016-12-20
      • 2019-10-09
      • 1970-01-01
      相关资源
      最近更新 更多