Search code examples
rggplot2dplyrr-factorfacet-grid

Problem with factor and reordering facet_grid


I have constructed a dataset from the gss data (https://gss.norc.org/) associating data in decades

env_data <- select(gss, year, sex, degree, natenvir) %>% na.omit()

env_datadecades <- env_data %>% 
                    mutate(decade=as.factor(ifelse(year<1980,
                                                   "70s", 
                                                    ifelse(year>1980 & year<=1990,
                                                           "80s", 
                                                            ifelse(year>1990 & year<2000, "90s", "00s")))))

I want to plot it with ggplot2 and facet_grid() and the order is not right so I made it as seen somewhere else

set.seed(6809)

env_datadecades$decade <- factor(env_datadecades$decade,
                                 levels = c("Seventies", "Eighties", "Nineties", "Twothous"))

It worked the first time but when I try to run the code again I get NA for all data in decade. What is happening?


Solution

  • I just made a simple dataset of years

    df <- data.frame(Years = sample(1970:2010, 20, replace = T))
    

    Convert it into the required factors by this method,

    df <- df %>%
      mutate(Decades = case_when(Years < 1980 ~ "Seventies",
                                 1980 <= Years  & Years < 1990 ~ "Eighties",
                                 1990 <= Years & Years < 2000 ~ "Nineties",
                                 2000 <= Years ~ "TwoThousands"))
    df$Decades <- factor(df$Decades, levels = c("Seventies", "Eighties", "Nineties", "TwoThousands"), ordered = T)
    

    and now try faceting.

    I think the problem with your code was that you gave the levels one set of names when you first converted the variables to a factor, and then in the second line of code, you give them another set of names. Stick to the same set, and it should work