0
votes

Let's say I have a dataset with percentages of males and females in each town, and groups of towns (let's say states):

town <- 1:4
females <- c(64.5, 86.3, 35.7, 45.6)
males <- 100 - females
state <- c(1, 2, 2, 1)
df <- c(town, females, males, state)

What I wanna do is generate a stacked horizontal bar chart using the Plotly package, in a way that I'll have 2 bars (one for each state) and the mean percentages of each gender in these bars. The sum of mean males and females should be 100%. I'm aware I'll have to transform the data I have, maybe use a group_by state, but I don't know exactly how.

Thanks in advance!

1

1 Answers

0
votes

I think you may have intended to set up your data frame like this:

df <- data.frame(town = town, females = females, males = males, state = factor(state))

instead of using c() which will create a vector.

Reshaping data could be done, using pivot_longer from dplyr so that you have a separate column for gender and percentage (within each town and state).

Then, you can compute the percentage_mean by grouping by state and gender.

library(tidyverse)
library(plotly)

df %>%
  pivot_longer(cols = c(females, males), names_to = "gender", values_to = "percentage") %>%
  group_by(state, gender) %>%
  summarise(percentage_mean = mean(percentage)) %>%
  plot_ly(
    x = ~percentage_mean,
    y = ~state,
    color = ~gender,
    type = "bar",
    orientation = "h"
  ) %>%
  layout(
    barmode = "stack"
  )

Plot

horizontal bar chart in plotly