问题描述
对不起,如果之前有人问过这个问题,试着环顾四周,但找不到任何东西。我有以下代码,它并排生成 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
解决方法
您可以添加 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)))