微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R:试图将 facet-wrap 的标题移到前面

如何解决R:试图将 facet-wrap 的标题移到前面

对不起,如果之前有人问过这个问题,试着环顾四周,但找不到任何东西。我有以下代码,它并排生成 5 个饼图,它们各自的标题在顶部,n 值在底部。 :

Testing <- data.frame(
  Letter = c("A","B","C","q","g"),Category = c("Category A","Category B","Category A","Category B"),Count = c(58,16,20,42,37,84,80,58,63),RawN = c("n = 100","n = 200","n = 300","n = 400","n = 500","n = 100","n = 500")
)


ggplot(Testing,aes(x = "",y = Count,fill = factor(Category))) + 
  geom_col(width = 1,position = "stack") +
  facet_wrap( ~ Letter,ncol=5)+
  theme_void()+
  theme(legend.position = "bottom")+
  scale_fill_manual(labels = c("Category A",values = c("blue","red"))+
  labs(fill = "")+
  coord_polar(theta = "y")+
  geom_text(aes(label = RawN),x = -Inf,y = Inf,hjust = 0.5,vjust = 7.9)

但是,字母“q”和“g”的标题被剪掉了。我试过使用条带文本,但无法正确显示字母。有没有办法将标题的图层移到前面,这样“q”和“g”就不会被饼图截断?

**编辑以修复被截断并尝试添加输出图像的 data.frame

PieChartOutput

解决方法

您可以添加 theme(strip.text.x=element_text(margin=margin(b=5))),它会调整您的图的边距,以便不覆盖标签:

ggplot(Testing,aes(x = "",y = Count,fill = factor(Category))) + 
  geom_col(width = 1,position = "stack") +
  facet_wrap( ~ Letter,ncol=5)+
  theme_void()+
  theme(legend.position = "bottom")+
  scale_fill_manual(labels = c("Category A","Category B"),values = c("blue","red"))+
  labs(fill = "")+
  coord_polar(theta = "y")+
  geom_text(aes(label = RawN),x = -Inf,y = Inf,hjust = 0.5,vjust = 7.9)+
  theme(strip.text.x=element_text(margin=margin(b=1)))

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。