R: 试图将 facet-wrap 的标题移到前面
R: Trying to move the title of a facet-wrap to the front
抱歉,如果之前有人问过这个问题,请尝试四处寻找但找不到任何东西。我有以下代码,它并排生成 5 个饼图,它们各自的标题在顶部,n 值在底部。 :
Testing <- data.frame(
Letter = c("A", "B", "C", "q", "g"),
Category = c("Category A", "Category B",
"Category A", "Category B",
"Category A", "Category B",
"Category A", "Category B",
"Category A", "Category B"),
Count = c(58, 16, 20, 42, 37,
42, 84, 80, 58, 63),
RawN = c("n = 100", "n = 200", "n = 300", "n = 400", "n = 500",
"n = 100", "n = 200", "n = 300", "n = 400", "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", "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)
但是,字母“q”和“g”的标题被截掉了。我试过玩条形文字,但无法正确显示字母。有没有办法将标题的图层移到前面,这样“q”和“g”就不会被 pie-chart 截断?
** 已编辑以修复被截断并尝试添加输出图像的 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)))
抱歉,如果之前有人问过这个问题,请尝试四处寻找但找不到任何东西。我有以下代码,它并排生成 5 个饼图,它们各自的标题在顶部,n 值在底部。 :
Testing <- data.frame(
Letter = c("A", "B", "C", "q", "g"),
Category = c("Category A", "Category B",
"Category A", "Category B",
"Category A", "Category B",
"Category A", "Category B",
"Category A", "Category B"),
Count = c(58, 16, 20, 42, 37,
42, 84, 80, 58, 63),
RawN = c("n = 100", "n = 200", "n = 300", "n = 400", "n = 500",
"n = 100", "n = 200", "n = 300", "n = 400", "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", "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)
但是,字母“q”和“g”的标题被截掉了。我试过玩条形文字,但无法正确显示字母。有没有办法将标题的图层移到前面,这样“q”和“g”就不会被 pie-chart 截断?
** 已编辑以修复被截断并尝试添加输出图像的 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)))