Nader Mehri
Nader Mehri

Reputation: 556

Error within ggplot: Unknown levels in `f`: NCG

Using the data sample and code below, I have created a plot shown in the image below. How can I get rid of NAs in the legend and have a plot with three values: Non_caregivers, Caregivers <14h/w, and Caregivers>=14h/w. I got a warning message of "Unknown levels in f: NCG". Also, I was wondering if there is any way to order the plots so that white's plot appears first.

My sample data

    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)

My code

   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"))

my plot

enter image description here

Upvotes: 0

Views: 388

Answers (1)

Edward
Edward

Reputation: 18708

For the facets, reordering can be done by releveling the levels of the factor and specifying the reference level.

For the legend, you're probably better to create a new variable as you did for race and then use that as the fill.

Race %>% 
  mutate(race=relevel(factor(str_extract(sample_label,"White|Black|Hispanic")), ref="White"),
         type=str_extract(sample_label, "NCG|caregivers<14h/w|caregivers>=14h/w")) %>%
  ggplot(aes(x =Age_Group_, y = meanTLE_, fill=type, group=sample_label)) + ...

The rest is the same as you have.

enter image description here

Upvotes: 1

Related Questions