Fixing X-axis of Horizontal Plotly Bar Chart

Victor

I'm using R's plotly package inside a Shiny Dashboard to create the funnel chart below.

Current plot.

enter image description here

There is a problem with this chart in that the x-axis displays negative values to the left of 0. Normally this wouldn't be a problem of course, but this is a funnel chart, so the values to the left of 0 aren't really negative, because they represent count data. I don't know how to fix it.

Desired plot.

enter image description here

The code is

output$plot_piramide_casos <- renderPlotly({

m <- list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

plot_ly(piramide_casos(), x = ~Freq_n, y = ~Faixa, type = 'bar', color = ~Sexo, colors = c("darkorange1", "dodgerblue3"), 
        text = ~paste("Faixa Etária: ", Faixa, '<br>Número Absoluto:', Freq, '<br>Proporção:', prop,"%"), hoverinfo = 'text')  %>% 
  layout(barmode='relative',
         xaxis = list(title = "População", tickformat = "digits", zeroline = FALSE),
         yaxis = list(title = "Faixa Etária", zeroline = FALSE)) %>%
  layout(title = "Casos confirmados de COVID-19", titlefont = list(size = 24, face="bold"))%>%
  layout(autosize = F, width = 545, height = 400, margin = m)

})
Duck

You can obtain the same plot combining ggplot2 and plotly using ggplotly. I have used some dummy data and dplyr for transforming variables:

library(ggplot2)
library(plotly)
library(dplyr)
#Data
df <- data.frame(Age=c('0-19','20-29','30-39','40-49','50-69','60-Inf',
                       '0-19','20-29','30-39','40-49','50-69','60-Inf'),
                 Pop=c(1000,3000,7000,5000,3000,800,
                       900,2500,6000,4000,2000,500),
                 Gender=c(rep('Male',6),rep('Female',6)))
#Code
Plot <- df %>%
  mutate(Age=factor(Age,levels = unique(Age),ordered = T),
         Pop=ifelse(Gender=='Male',-Pop,Pop)) %>%
  ggplot(aes(x=Age,y=Pop,fill=Gender))+
  geom_bar(stat = 'identity')+
  scale_y_continuous(labels = function(x) abs(x))+
  coord_flip()+
  scale_fill_manual(values=c('orange','blue'))+
  theme_bw()
#Plotly
ggplotly(Plot)

Output:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ZingCharts horizontal stacked bar chart X-Axis labels being cut off

ggvis - Interactive X axis for bar chart

MP android chart displaying float values at X-Axis of Horizontal bar Chart?

Hide bottom x axis in horizontal bar chart?

Plotly horizontal bar - display ALL y axis labels

Display y axis value horizontal line drawn In bar chart

Ordering columns in Plotly horizontal bar chart

Offline Plotly: Generate bar chart of 2 columns on (y axis) & 1 column on ( x axis)

Horizontal axis for bar chart

R: Horizontal Stacked Bar Chart using Plotly()

to increase space between x axis and first horizontal bar in chart js

How to Arrange the x axis on a Plotly Bar Chart with R

D3 Horizontal stacked bar chart axis cut off

Horizontal bar chart y axis alignment

How to add a line on x-axis on a horizontal bar chart in d3

JFreeChart - horizontal stacked bar chart with date axis

Custom D3 `tickValue` on x axis -- horizontal bar chart

Horizontal Stacked Bar Chart with Two Y Axis Issues

Horizontal bar-chart: mis-matched axis units

Rotate x axis text on Horizontal bar chart

Reorder x axis in bar chart in ggplot

How can I change the order of traces in horizontal Plotly bar chart?

How to move axis label to opposite side in Altair horizontal bar chart

How to remove x-axis tick labels in a stacked-and-grouped bar chart using Plotly

Bar chart and vertical line with discrete axis in plotly

Make bar chart's x-axis markers horizontal or 45 degree readable in Python Altair

Bar chart labeled with % relative to the Months in X axis

Create dash plotly stack bar chart that has interactive x axis based on dropdown values

Customizing x-axis for Tornado chart in Plotly