从分组的facet_grid条形图

时间:2017-03-31 05:04:45

标签: r ggplot2 geom-bar facet-grid

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

我需要创建一个图表来删除X上未使用的级别,以便强烈不同意,不同意和Mildyl不同意被归入“批评者”的方面,然后不确定和温和地同意被分为“被动”的方面',同意和非常同意归为'推广者'。

这是数据的输入

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标签在底部出现一次。考虑到每个标签的类别数量,它如何间隔小平面也很奇怪。

有什么想法吗?

enter image description here

1 个答案:

答案 0 :(得分:3)

问题在于您使用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()) 

enter image description here