Используя образец данных и код ниже, я создал график, показанный на рисунке ниже. Как я могу избавиться от АН в легенде и получить сюжет с тремя значениями: Non_caregivers, Caregivers <14h / w и Caregivers> = 14h / w. Я получил предупреждение «Неизвестные уровни в f
: NCG». Кроме того, мне было интересно, есть ли способ упорядочить участки так, чтобы сначала отображался график белых.
Мои данные выборки
sample_label<-c("24.NCG White","24.NCG White","24.NCG White",
"25.caregivers<14h/w White","25.caregivers<14h/w White",
"25.caregivers<14h/w White","26.caregivers>=14h/w White",
"26.caregivers>=14h/w White","26.caregivers>=14h/w White",
"27.NCG Black","27.NCG Black","27.NCG Black",
"28.caregivers<14h/w Black", "28.caregivers<14h/w Black",
"28.caregivers<14h/w Black", "29.caregivers>=14h/w Black",
"29.caregivers>=14h/w Black","29.caregivers>=14h/w Black",
"30.NCG Hispanic","30.NCG Hispanic","30.NCG Hispanic",
"31.caregivers<14h/w Hispanic","31.caregivers<14h/w Hispanic",
"31.caregivers<14h/w Hispanic","32.caregivers>=14h/w Hispanic",
"32.caregivers>=14h/w Hispanic","32.caregivers>=14h/w Hispanic")
Age_Group_<-c("50-51","60-61","70-71","50-51","60-61","70-71",
"50-51","60-61","70-71","50-51","60-61","70-71",
"50-51","60-61","70-71","50-51","60-61","70-71",
"50-51","60-61","70-71","50-51","60-61","70-71",
"50-51","60-61","70-71")
meanTLE_<-c(34.3,25.5,17.2,36,26.8,18.1,35.1,26.1,17.7,33.3,24.6,
16.7,35.1,26.1,17.7,34.1,25.4,17.2,35.6,26.5,18,37,27.7,18.7,
36.4,27.1,18.3)
meanHLE_<-c(27.5,20.3,13.7,29.6,22.1,14.8,27.9,20.7,13.9,25.4,18.8,
12.6,27.7,20.6,13.8,25.8,19.1,12.9,23.1,17.1,11.5,25.3,
18.9,12.7,23.3,17.3,11.6)
Мой код
library(tidyverse)
pd = position_dodge(0.9)
Race %>%
mutate(race=str_extract(sample_label,"White|Black|Hispanic"),
sample_label=gsub("White", "", sample_label),
sample_label=gsub("Black", "", sample_label),
sample_label=gsub("Hispanic", "", sample_label),
sample_label=fct_relevel(sample_label, "NCG")) %>%
ggplot(aes(x =Age_Group_, y = meanTLE_, fill=sample_label, group=sample_label)) +
# Dodge value labels and bars by same amount
geom_col(position = pd, width=0.85) +
geom_text(aes(label=sprintf("%1.1f", meanTLE_)), hjust=0,
colour = "black", fontface = "bold", size=3, angle = 90,
# Dodge value labels and bars by same amount
position = pd) +
geom_col(aes(y=meanHLE_), width=0.5, size=0.2, colour="grey50", fill="white", position=pd) +
geom_text(aes(label=sprintf(paste0("HLE=", meanHLE_)),
y=meanHLE_, colour=sample_label), position=pd, hjust=2,
colour = "black", fontface = "bold", size=3, angle = 90) +
# geom_text(data=. %>%
# group_by(sex) %>%
# filter(Age_Group_=="70-71", grepl(">=14", sample_label)) %>%
# ungroup %>%
# pivot_longer(starts_with("mean")), hjust=0, colour="grey30", size=3,
# aes(x=3.5, y=value, label=gsub("mean(.*)_", "\\1", name))) +
facet_grid(cols=vars(race)) +
scale_y_continuous(limits=c(0,41), expand=c(0,0)) +
#expand_limits(x=3.9) +
scale_colour_manual(values=hcl(seq(15,375,length=4)[1:3], 100, 80)) +
labs(x = "Age Groups", y = "Total Life Expectancy (Years)") +
theme_bw() +
theme(legend.title=element_blank(),
legend.text = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5, size = 15, colour = "Black", face = "bold"),
plot.caption = element_text(hjust = 0, color = "black", face = "bold", size=12.5))+
scale_fill_discrete(name = "variable",
labels = c("Non-caregivers", "Caregivers <14h/w", "Caregivers >=14h/w"))
мой сюжет