使用gather和ggplot保持因子水平

时间:2018-05-14 12:49:38

标签: r ggplot2

为了获得包含所有因子类别的图,我需要在使用函数收集后保持因子水平。我的尝试现在不成功。任何帮助表示赞赏

数据结构:

 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    

可重复数据:

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

我的ggplot代码:

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

结果

enter image description here 预期结果:该图应保留所有4个因子水平。

0 个答案:

没有答案