here is something, hope it helps!
long story short:
- I think you did something too complex if what you want is just survivors rate, no need to use
gather
(if you really want to, pivot_longer
is more recommended). I used group_by
and summarize
instead, which is what you need for aggregations.
- Since both graphics haven't the same x, it's simpler to make 2 graphics, one for class, the other for sex. Then combine them with +, having
patchwork
loaded.
- beware,
geom_col
is what you want, geom_bar
is plotting counts
- Regarding your label issue, just use
geom_text
welcome in R commmunity!
##we need the tidyverse package for data manipulation and ggplot
library(tidyverse)
##since we'll make 2 different graphs, patchwork will allow us to combine them
library(patchwork)
#data reproduction
ds <- data.frame(Sex = sample(c("male", "female"), size = 100, replace = TRUE),
Survived = sample(c("survived", "dead"), size = 100, replace = TRUE),
Class = sample(c("1st", "2nd", "3rd", "crew"), size = 100, replace = TRUE))
#I start with classes : group_by allows us to compute the rate of each group
#summarize uses these group to compute the rate : number of survivors / number in the group (and not NA)
class.ds <- ds %>%
select(Class, Survived) %>%
group_by(Class) %>%
summarize(surv_rate = sum(Survived == "survived") / sum(!is.na(Survived)))
#we do the same for the sex
sex.ds <- ds %>%
select(Sex, Survived) %>%
group_by(Sex) %>%
summarize(surv_rate = sum(Survived == "survived") / sum(!is.na(Survived)))
#now the plots : class is our x axis, the rate is the y axis.
#geom_col is used for the bars, geom_text is for the labels
#of course, you can then add color etc, here I keep it simple
class.plot <- class.ds %>% ggplot(aes(Class, surv_rate)) +
geom_col() +
geom_text(aes(label = round(surv_rate, 2)), nudge_y = 0.02)
#same thing for the sex
sex.plot <- sex.ds %>% ggplot(aes(Sex, surv_rate)) +
geom_col() +
geom_text(aes(label = round(surv_rate, 2)), nudge_y = 0.02)
#now we just need to group the graphs with patchowork
class.plot + sex.plot
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…