从分组 facet_grid 条形图中删除未使用的因素

Removing unused factors from grouped facet_grid bar plot

我已经通读了该搜索结果的第一页,但似乎没有任何效果。

我需要创建一个图来删除 X 上未使用的级别,以便将强烈不同意、不同意和 Mildyl 不同意分组到 'detractors' 的方面,然后将不确定和轻微同意分组到 'detractors' 的方面'Passive' 的方面,同意和强烈同意被分组到 'Promoters'。

这是数据的输出

structure(list(area = c("NPS.recomm", "invest", "commit", "involve", 
"all.consid", "exit.in", "FBM.recomm", "NPS.recomm", "invest", 
"commit", "involve", "all.consid", "exit.in", "FBM.recomm", "NPS.recomm", 
"invest", "commit", "involve", "all.consid", "exit.in", "FBM.recomm"
), response.cat = c("Strongly \ndisagree", "Disagree", "Mildly \ndisagree", 
"Uncertain", "Mildly \nagree", "Agree", "Strongly \nagree", "Strongly \ndisagree", 
"Disagree", "Mildly \ndisagree", "Uncertain", "Mildly \nagree", 
"Agree", "Strongly \nagree", "Strongly \ndisagree", "Disagree", 
"Mildly \ndisagree", "Uncertain", "Mildly \nagree", "Agree", 
"Strongly \nagree"), response.set = c("Detractors", "Detractors", 
"Detractors", "Passive", "Passive", "Promoters", "Promoters", 
"Detractors", "Detractors", "Detractors", "Passive", "Passive", 
"Promoters", "Promoters", "Detractors", "Detractors", "Detractors", 
"Passive", "Passive", "Promoters", "Promoters"), split = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L), .Label = c("curr.score", "prior.score", "bench.score"
), class = "factor"), score = c(7, 6, 3, 15, 16, 39, 14, 5, 4, 
4, 15, 22, 33, 18, 14, 10, 6, 16, 15, 29, 10)), row.names = c(NA, 
-21L), .Names = c("area", "response.cat", "response.set", "split", 
"score"), class = "data.frame")

这是当前代码

col.set <- c("dodgerblue4", "#FF9933", "gold")
plot.labels <- c("group1","group2","group3")
curr.plot <- ggplot(plot.data, aes(response.cat, score)) +   
  facet_grid(. ~ response.set, scales="free_x", space = "free_x") +
  geom_bar(aes(fill = split), position = "dodge", stat="identity")+
  scale_fill_manual(values=c(col.set), labels= plot.labels )+
  ylim(c(0,100))+
  theme(legend.title = element_blank(), legend.position = "bottom", 
        legend.text=element_text(colour= "gray23"), legend.key.height=unit(3,"mm")) +
  theme(axis.title= element_blank()) +
  scale_x_discrete(limits = c("Strongly \ndisagree","Disagree","Mildly \ndisagree","Uncertain","Mildly \nagree","Agree","Strongly \nagree"))+
  geom_text(aes(fill = split, label = paste0(score,"%")), colour = "gray23", vjust=-1, position=position_dodge(.9),size=3)+  
  theme(panel.grid.minor.y = element_blank()) +
  theme(panel.grid.major.y = element_line(colour = "gray")) +
  theme(panel.grid.major.x =element_blank(), panel.grid.minor.x =element_blank()) +
  theme(panel.background = element_rect(fill="white")) +
  theme(axis.text.x = element_text(colour = "gray23")) +
  theme(axis.text.y = element_text(colour = "gray23")) +
  theme(axis.ticks.y=element_blank()) 

产生下面的结果,你可以看到贬低者方面看起来是正确的,但其他两个方面包含未使用的因素。我只希望 X 标签在 bottom.Also 中出现一次,这很奇怪它如何在给定每个方面的类别数量的情况下间隔这些方面。

有什么想法吗?

问题出在您对 scale_x_discrete 的使用上。您可以将 response.cat 转换为有序因子:

plot.data$response.cat <- factor(plot.data$response.cat, levels = c("Strongly \ndisagree","Disagree","Mildly \ndisagree","Uncertain","Mildly \nagree","Agree","Strongly \nagree"))

ggplot(plot.data, aes(response.cat, score)) +   
  facet_grid(. ~ response.set, scales="free_x", space = "free_x") +
  geom_bar(aes(fill = split), position = "dodge", stat="identity")+
  scale_fill_manual(values=c(col.set), labels= plot.labels )+
  ylim(c(0,100))+
  theme(legend.title = element_blank(), legend.position = "bottom", 
        legend.text=element_text(colour= "gray23"), legend.key.height=unit(3,"mm")) +
  theme(axis.title= element_blank()) +
  geom_text(aes(fill = split, label = paste0(score,"%")), colour = "gray23", vjust=-1, position=position_dodge(.9),size=3)+  
  theme(panel.grid.minor.y = element_blank()) +
  theme(panel.grid.major.y = element_line(colour = "gray")) +
  theme(panel.grid.major.x =element_blank(), panel.grid.minor.x =element_blank()) +
  theme(panel.background = element_rect(fill="white")) +
  theme(axis.text.x = element_text(colour = "gray23")) +
  theme(axis.text.y = element_text(colour = "gray23")) +
  theme(axis.ticks.y=element_blank())