1

I'm trying to implement these 3 lines of code, which is my desired output, in 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()

In Shiny, I want a user to be able to choose starting and ending points of a lollipop graph. I also want a user to be able to choose a categorical variable (among many categorical variables -- even though for now, the example data only has one categorical variable) that would show up on the y-axis. Below is what I worked on so far, literally replacing all variables to input variables (so I think it should work but it doesn't..)

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)

Could anyone help why am I not getting the desired output? :(

1 Answer 1

3

The desired plot doesn't show up because you are calling ggplot in a different way. In your shiny app the selected column names are strings:

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

Replace the aes with aes_string and it will work.

ggplot(health, aes_string(x='pct_2013', xend='pct_2014', y='Area'))+
       geom_dumbbell()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.