在ggplot aes中使用闪亮的输入值

问题描述 投票:1回答:1

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

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

在“闪亮”中,我希望用户能够选择棒棒糖图的起点和终点。我还希望用户能够选择一个将显示在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个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.