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:
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
)
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.