0

I already have one shiny webpage working perfectly, now I need to make it have a dual-axis. I have years on the X-axis, and GDP per Capita on the Y-axis currently. Now my question is how do I make another numerical value show up on the same line graph? I make it so I can show up to 5 countries' GDP line charts at the same time on the graph currently. I am assuming this dual-axis feature code will go in the "server" section of my code? for ease of coding my files would be Aid and GDP_NZ. GDP_NZ is already in the code, I want to add different values from the Aid file ontop of the GDP so I can see multiple values on the line chart through the shiny webapp.

server <- function(input, output) {
  output$countryOutput <- renderUI({
    #dropdown with select to pick country or can type name of country and choose from there
    selectizeInput("countryInput", "Country Name(s) of Interest",
                   choices = unique(GDP_NZ$Country),
                   selected = NULL,
                   multiple = T,
                   options = list(maxItems = 5,
                                  placeholder = "Pick a Country"))
    
  })  
  
  filtered <- reactive({
    if (is.null(input$countryInput)) {
      return(NULL)
    }    
    
    GDP_NZ %>%
      filter(
        Country == input$countryInput
      )
  })
  
  output$coolplot <- renderPlot({
    if (is.null(filtered())) {
      return()
    }
    #axes manipulation and alteration
   ggplot(filtered(), aes(x = Year, y = Value, color = Country)) +
      geom_line() +
      geom_point() +
      theme(axis.title.x = element_text(family = "Times", face = "plain", size = 14)) +
      theme(axis.title.y = element_text(family = "Times", face = "plain", size = 14)) +
      labs(y = "GDP per Capita", x = "Year")
    
    
  })
  
  output$results <- renderTable({
    filtered()
  })
}

shinyApp(ui = ui, server = server)


  • Can you provide a [mcve] of your data and be a bit more clear about where, specifically, your problem is? Is your problem with [how to show 2 y-axes with ggplot?](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) Is it with loading specific data files based on user input? Is it joining the 2 data frames into a single one to graph with ggplot? – divibisan Jul 08 '20 at 16:59
  • Im just cofused on how I would add a dual axis in R Shiny. Maybe I'm over thinking things, but how would I do a do a dual axis when I am taking values from two different data frames ? or is it in the nature of ggplot that allows me to do it by adding another " aes() " section –  Jul 08 '20 at 17:03
  • Making the plot in shiny is exactly the same as in normal R, so figure out how to make the plot you want from the data you have in RStudio, then move the code to shiny. ggplot2 has a philosophical objection to showing different y-axes (that aren't a transformation of each other) on the same plot, but [this quesiton](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) provides a few workarounds. – divibisan Jul 08 '20 at 17:06
  • Or am I misreading your problem? Does all your data have the same y-variable ("Value"), but the problem is that each country's data is stored in a separate file and you want to: 1) have the user pick a list of countries, 2) load those country's data files, 3) join those data frames together, and 4) plot them on the same plot with each country in a different color. Is that the goal? – divibisan Jul 08 '20 at 17:08
  • Okay I think I getting something wrong in my head, but no the data has different names for the Y variable and they are two separate files. For your list: yes all 4 are correct. P.S. I changed the name in the file itself to be "GDP" not "Value" –  Jul 08 '20 at 17:58
  • 1
    So what you could do is give a named list to `selectizeInput` containing the filenames for each data file, then use `lapply` to load each one, rename them to be consistent, then use `bind_rows` (with `.id = 'Country'` to create a country variable) to join them into one long-form dataframe, which you can pass to `ggplot` using `color = Country` to make a different line for each country. Do you need to use separate files with inconsistent naming for each country, though? You could also preprocess the data as a single file or a `.RData` file – divibisan Jul 08 '20 at 20:06
  • If you want more help, please provide a [mcve] of your data files. Ideally, you'd paste them into the question as raw text – divibisan Jul 08 '20 at 20:07

0 Answers0