简体   繁体   中英

Plotly stacked horizontal bar chart as percentages in R

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!

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

plotly 中的水平条形图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM