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 :
but I want something like this (maked with notebookà :