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

0

Issue

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

Solution

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

Answered By – jpdugo17

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More