Hi everyone, I am recently learning to use Rstudio, can someone give me a clue of how to achieve a graph like this

Each color is a different site.

Thank you in advance

Hi everyone, I am recently learning to use Rstudio, can someone give me a clue of how to achieve a graph like this

Each color is a different site.

Thank you in advance

We don't have that data, so I can't reproduce that plot exactly, but here's essentially the same thing using the `Titanic`

dataset. The below does the following:

- Calculate the total number of men and the total number of women per each class
- Calculate the total number of people in each class regardless of Sex
- Divide Step 1 by Step 2

Then that gives you all the data you need, so I would start by building a regular column chart, then use `coord_flip`

to switch the x and y axes.

```
library(tidyverse)
df <- Titanic %>%
as_tibble %>%
group_by(Class, Sex) %>%
summarize(
num_per_sex_per_class = sum(n)
) %>%
group_by(Class) %>%
mutate(
num_in_class = sum(num_per_sex_per_class),
prop_sex = num_per_sex_per_class / num_in_class
)
df %>%
ggplot(
aes(
x = Class,
y = prop_sex
)
) +
geom_col(
aes(
fill = Sex
)
) +
coord_flip() +
scale_y_continuous(
labels = scales::percent
)
```

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.