# Make histogram with plotlyR in a shiny app

**URL:** <https://community.plotly.com/t/make-histogram-with-plotlyr-in-a-shiny-app/79817>\
**Category:** Plotly R\
**Tags:** question, bug-reporter\
**Created:** [November 2, 2023, 3:56pm UTC](https://community.plotly.com/t/make-histogram-with-plotlyr-in-a-shiny-app/79817 "2023-11-02T15:56:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saida](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Saida](https://community.plotly.com/u/Saida)\
**Post date:** [November 2, 2023, 3:56pm UTC](https://community.plotly.com/t/make-histogram-with-plotlyr-in-a-shiny-app/79817/1 "2023-11-02T15:56:43Z")

</div>

Hello, I tried to make a plotly in a shiny app but the result is not what I want.  
Indeed, the graph displays a bar at 100% instead of performing the barplot.

The Code :

shinyUI(fluidPage(

```
# Application title
titlePanel("Comparaison population"),
tags$br(),
tags$hr(),

  
    # Sidebar with a select inputs to choose variables to plot
    sidebarLayout(
        sidebarPanel(
            selectInput("x_variable", 
                        "X Variable",
                        c("anc_soc_mois",
                         "age_soc")
                       #names(df)
                       )
           
        ),
        
        # Show a plot with configurable axes
        mainPanel(
           plotlyOutput("varPlot")
        )
    ),
    tags$hr()

```

)  
)

#cols\<-names(df)  
groupA ← df %\>% filter(population == “Ventes”)  
groupB ← df %\>% filter(population == “Portefeuille”)

shinyServer(function(input, output) {

output$varPlot ← renderPlotly({

```
 # Calculer les pourcentages par groupe
 totalA <- nrow(groupA)
 totalB <- nrow(groupB)

percentageA <- (groupA %>% 
   count(input$x_variable) %>% 
   na.omit()%>%
   mutate(percentage = n / totalA) %>% 
   mutate(selectedVar = input$x_variable)) %>% 
   arrange(selectedVar)
 
percentageB <- (groupB %>% 
   count(input$x_variable) %>% 
   na.omit()%>%
   mutate(percentage = n / totalB) %>% 
   mutate(selectedVar = input$x_variable)) %>% 
   arrange(selectedVar)

```

p5 ← plot\_ly(alpha = 0.6) %\>% # Ne nécessite pas “nbinsx = 30”  
add\_trace(  
x = ~percentageA$selectedVar,  
y = ~percentageA$percentage \* 100,  
name = “Ventes”,  
type = “bar”,  
text = ~paste0(round(percentageA$percentage,2) \* 100, “%”)  
) %\>%  
add\_trace(  
x = ~percentageB$selectedVar,  
y = ~percentageB$percentage \* 100,  
name = “Portefeuille”,  
type = “bar”,  
text = ~paste0(round(percentageB$percentage,2) \* 100, “%”)  
) %\>%  
layout(  
barmode = “overlay”,  
title = “Répartition par type de population”,  
xaxis = list(title = “X Variable”),  
yaxis = list(title = “Pourcentage”,  
zeroline = FALSE)  
)

p5

```
})

```

})

the result :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/d/3/d3384a0a5cb61754847b3030e308469c1eb9d0a3.png)

but I want something like this (maked with notebookà :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/a/0/a0da766cb0234f82549547c8ba71723c46a42893.png)

---

<div class="post-metadata">

**Author:** ![ismirsehregal](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/ismirsehregal/32/6794_2.png) [@ismirsehregal](https://community.plotly.com/u/ismirsehregal)\
**Post date:** [November 7, 2023, 11:57am UTC](https://community.plotly.com/t/make-histogram-with-plotlyr-in-a-shiny-app/79817/2 "2023-11-07T11:57:03Z")

</div>

[To create histograms](https://plotly.com/r/histograms/) you’ll need to use `type = "histogram"` instead of `type = "bar"` in you `plot_ly()` call.
