how can I compare between two ggplots in R studio in layers ( in other words having two plots over each other and compare them through my inputs )?

Viewed 119

I have two output plots and I want to get them above each other, so I can compare between them through the selected inputs input$sel and input$sel2.

I mean with above each other is one should be in the background and the other one should be another layer on it transparent, so one can see both plots at the same time.

If data are necessary, here is data.csv file : https://impfdashboard.de/static/data/germany_vaccinations_by_state.tsv

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

#Summarize Data and then Plot 
data <- reactive({
req(input$sel)
df <- germany_vaccinations_k %>%  group_by(code) %>% summarise( output = get(input$sel))
print(df)
})

data2 <- reactive({
req(input$sel2)
df <- germany_vaccinations_k %>%  group_by(code) %>% summarise( output = get(input$sel2))
print(df)
})

#Plot 1
output$plot <- renderPlot({  
g <- ggplot( data(), aes( y = output   ,x = code ,ho = factor(code) )  ) 
g + geom_bar( stat = "sum" )

output$plot2 <- renderPlot({  
g <- ggplot( data(), aes( y = output   ,x = code ,ho = factor(code) )  ) 
g + geom_bar( stat = "sum" )
})

ui <- basicPage(   
#first-input
selectInput(inputId = "sel",  label = "Möglichkeit auswählen",
list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),

#second-input
selectInput(inputId = "sel2",  label = "Möglichkeit auswählen",
list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),

#the both outputs 
plotOutput("plot")
plotOutput("plot2")
)

1 Answers

We can combine both plots and play around with alpha argument. I added two sliders that enables the user to control the level of transparency of both plots.

  output$plot <- renderPlot({
    ggplot() +
      geom_bar(data = data(), aes(y = output, x = code), stat = "sum", alpha = .5) +
      geom_bar(data = data2(), aes(y = output, x = code), stat = "sum", alpha = .5, fill = "lightblue")
  })

app:

germany_vaccinations_k <- read_tsv("germany_vaccinations_by_state.tsv")

ui <- basicPage(
  # first-input
  selectInput(
    inputId = "sel", label = "Möglichkeit auswählen",
    list("vaccinationsTotal", "peopleFirstTotal", "peopleFullTotal", "peopleBoosterTotal")
  ),

  # second-input
  selectInput(
    inputId = "sel2", label = "Möglichkeit auswählen",
    list("vaccinationsTotal", "peopleFirstTotal", "peopleFullTotal", "peopleBoosterTotal")
  ),
  sliderInput("alpha_sel", "Select First Alpha", min = .01, max = 1, value = 0.5),
  sliderInput("alpha_sel2", "Select Second Alpha", min = .01, max = 1, value = 0.8),


  # the both outputs
  plotOutput("plot")
)



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

  # Summarize Data and then Plot
  data <- reactive({
    req(input$sel)
    df <- germany_vaccinations_k %>%
      group_by(code) %>%
      summarise(output = get(input$sel))
    print(df)
  })

  data2 <- reactive({
    req(input$sel2)
    df <- germany_vaccinations_k %>%
      group_by(code) %>%
      summarise(output = get(input$sel2))
    print(df)
  })


  output$plot <- renderPlot({
    ggplot() +
      geom_bar(data = data(), aes(y = output, x = code), stat = "sum", alpha = input$alpha_sel) +
      geom_bar(data = data2(), aes(y = output, x = code), stat = "sum", alpha = input$alpha_sel2, fill = "lightblue")
  })
}

shinyApp(ui, server)

enter image description here

Related