我有下图。如何将第二行向右对齐?谢谢!
最佳答案
根据 this回答您可以将绘图作为网格对象进行操作。
library(ggplot2)
library(dplyr)
library(grid)
# inject blank level into factor
d1 <- mutate(mtcars,
carb = factor(replace(carb, carb == 8, 6),
levels = c(1:3, 0, 4, 6),
labels = c("Replay-Say", "Replay-Project", "Replay-Proj-Say",
"", "Project", "Project-Say")))
p1 <- ggplot(data = d1,
mapping = aes(x = factor(cyl))) +
geom_bar() + xlab("") + ylab("") + facet_wrap(~carb, drop = FALSE)
p1
根据绘图的布局,您需要确定要删除的面板名称和刻度线。
g1 <- ggplotGrob(p1)
g1$layout$name
# [1] "background" "panel-1-1" "panel-3-1" "panel-1-2" "panel-2-2"
# [6] "panel-3-2" "axis-t-1-2" "axis-t-2-2" "axis-t-3-2" "axis-t-1-1"
# [11] "axis-t-2-1" "axis-t-3-1" "axis-b-1-2" "axis-b-2-2" "axis-b-3-2"
# ...
然后,您可以从图中删除这些。字母分配似乎是顶部(“t”)、底部(“b”)、左侧(“l”)、右侧(“r”)。blank_panel_grobs <- c("panel-2-1", "strip-t-1-2", "axis-b-1-2", "axis-l-2-1")
blank_panel_index <- g1$layout$name %in% blank_panel_grobs
g1$layout <- lapply(g1$layout, function(x) x[!blank_panel_index])
g1$grobs <- g1$grobs[!blank_panel_index]
grid.draw(g1)
关于r - 在facet_wrap中,如何将第二行向右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68216901/