In order to get plots with all the factor categories, i need to keep the factor level after having used the function gather. My attempts are for now unsuccessful. Any help is appreciated
Data structure:
A tibble: 7 x 4
`difficulty_pre_How was the pace of the class?` `difficulty_pre_How muc… `difficulty_pre_How m… `difficulty_pre_Conside…
<chr> <chr> <chr> <chr>
1 Fast 26-50% 76-90% Somewhat challenging
2 About right 51-75% 51-75% About right
3 About right 91-100% 76-90% About right
4 About right 76-90% 51-75% About right
5 About right 91-100% 76-90% Somewhat challenging
6 About right 51-75% 91-100% Somewhat challenging
7 Fast 76-90% 51-75% Somewhat challenging
Reproducible data:
structure(list(`difficulty_pre_How was the pace of the class?` = c("Fast",
"About right", "About right", "About right", "About right", "About right",
"Fast"), `difficulty_pre_How much of the suggested reading list did you get through?` = c("26-50%",
"51-75%", "91-100%", "76-90%", "91-100%", "51-75%", "76-90%"),
`difficulty_pre_How much of the course material was new to you?` = c("76-90%",
"51-75%", "76-90%", "51-75%", "76-90%", "91-100%", "51-75%"
), `difficulty_pre_Considering your background, how did you find the level of the course?` = c("Somewhat challenging",
"About right", "About right", "About right", "Somewhat challenging",
"Somewhat challenging", "Somewhat challenging")), .Names = c("difficulty_pre_How was the pace of the class?",
"difficulty_pre_How much of the suggested reading list did you get through?",
"difficulty_pre_How much of the course material was new to you?",
"difficulty_pre_Considering your background, how did you find the level of the course?"
), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"
))
My ggplot code:
data%>%
na.omit() %>%
mutate_at(vars(2:3), funs(factor(., levels = c("1-25%", "26-50%", "51-75%", "76-90%","91-100%"), ordered = T)))%>%
mutate_at(vars(1), funs(factor(., levels = c("Too slow", "Slow", "About right", "Fast","Too fast"), ordered = T)))%>%
mutate_at(vars(4), funs(factor(., levels = c("Very easy", "Somewhat too easy", "About right", "Somewhat challenging","Very challenging"), ordered = T))) %>%
gather(factor_key = TRUE) %>%
mutate(key = str_sub(key, start = 16)) %>%
ggplot(aes(x = value)) +
geom_bar(aes(y = (..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])) +
scale_y_continuous(labels=percent,limits = c(-0, 1)) +
scale_x_discrete(drop=FALSE) +
ylab("Relative Frequencies (%)") +
xlab("") +
facet_wrap(~ key, scale='free_x') +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 0.9))+
theme(strip.text = element_text(size=6))
Results
Desired outcome: The plots should retain all 4 factors levels.