Search code examples
rggplot2shinyhistogram

Is it possible to plot two individual histograms with the same axis in Shiny?


I am working in Shiny and I want to plot two INDIVIDUAL histograms with the same scale (x and y axis) to be able to compare them.

I found this interesting post How to plot two histograms on the same axis scale? and I thought that it could work for my case.. In fact, I tried to do it in a simple script (without shiny) and it was working. However, when I tried to do it in Shiny I was receiving a lot of errors and I had a lot of problems.

On the other hand the solution in that post, it changes a bit how I want to visualize the histograms and I would like to see the histograms individually, if it is possible.

Do you know if there is a way to do this thing in Shiny?

Here I have written a simple code similar to mine.

library(shiny)
library(ggplot2)
library(scales)

ui <- fluidPage(
  
  titlePanel("Histogram"),
  
  sidebarLayout(
    sidebarPanel(
    ),
    
    mainPanel(
      plotOutput("hist_1"),
      plotOutput("hist_2")
    )
  )
)

val1 <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.matrix(val1)          

val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.matrix(val2)

server <- function(input, output) {
  
  output$hist_1 <- renderPlot({

    pp <- qplot(df1, geom = "histogram", bins = 10, xlab="values", 
                ylab="Frequency", main="Histogram 1",
                fill=I("red"), col=I("black"), alpha=I(0.4))
    
    pp + scale_x_continuous(breaks=pretty(df1, n=10))
  })
  
  output$hist_2 <- renderPlot({

    pp <- qplot(df2, geom = "histogram", bins = 10, xlab="values", 
                ylab="Frequency", main="Histogram 2",
                fill=I("blue"), col=I("black"), alpha=I(0.4))
    
    pp + scale_x_continuous(breaks=pretty(df2, n=10))
  })
  
}

shinyApp(ui = ui, server = server)

Thanks very much in advance,

Regards


Solution

  • You may try the use of facets :

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    ui <- fluidPage(
      
      titlePanel("Histogram"),
      
      sidebarLayout(
        sidebarPanel(
        ),
        
        mainPanel(
          plotOutput("hist")
        )
      )
    )
    
    
    server <- function(input, output) {
      
      output$hist <- renderPlot({
        data <- bind_rows(lst(df1, df2), .id = 'id')
       ggplot(data) + aes(value, fill = id) + 
        geom_histogram(bins = 10, col= "black", alpha=0.4) + 
        facet_wrap(~id) + 
        scale_fill_manual(values = c('red', 'blue')) + 
        scale_x_continuous(breaks=pretty(data$value, n=10))
      })
      
      
    }  
    shinyApp(ui = ui, server = server)
    

    enter image description here

    data

    df1 <- data.frame(value = val1)   
    df2 <- data.frame(value = val2)