strip.position 外部两列 facet_wrap 情节
strip.position external in two columns facet_wrap plot
在两列 facet_wrap 图中,将小平面的名称和轴放在左列的左侧,右列的右侧,顺序为有一个紧凑的情节,小平面靠在一起。
ggplot(economics_long, aes(date, value)) +
geom_line() +
labs(y="") +
facet_wrap(~variable, scales = "free_y", ncol = 2, strip.position = "left") +
theme(strip.background = element_blank(), strip.placement = "outside")
据我所知,在 vanilla ggplot2 中没有办法做到这一点。如果您对 gtables 很满意table,您可能会发现以下方法可行。
library(ggplot2)
# Base plot
p <- ggplot(economics_long, aes(date, value)) +
geom_line() +
labs(y="") +
theme(strip.background = element_blank(), strip.placement = "outside")
# Left aligned strips/axes
left <- p +
facet_wrap(~variable, scales = "free_y", ncol = 2, strip.position = "left")
# Right aligned strips/axes
right <- p +
facet_wrap(~variable, scales = "free_y", ncol = 2, strip.position = "right") +
scale_y_continuous(position = "right")
# Convert to gtables
left <- ggplotGrob(left)
right <- ggplotGrob(right)
# Combine parts of left and right
# Column numbers found by browsing through layout
gt <- cbind(left[, 1:7], right[, 9:ncol(right)])
# Render
grid::grid.newpage(); grid::grid.draw(gt)
由 reprex package (v2.0.1)
于 2021-10-20 创建
要以比手动判断 table 布局更编程的方式找到面板位置,我最好的猜测是这样做:
panels_left <- panel_cols(left)$l
panels_right <- panel_cols(right)$l
# The -2 is to include the left axis space (zero width because of absence)
# and the panel spacing
gt <- cbind(left[, 1:panels_left[1]],
right[, (panels_right[2]-2):ncol(right)])
grid::grid.newpage(); grid::grid.draw(gt)
在两列 facet_wrap 图中,将小平面的名称和轴放在左列的左侧,右列的右侧,顺序为有一个紧凑的情节,小平面靠在一起。
ggplot(economics_long, aes(date, value)) +
geom_line() +
labs(y="") +
facet_wrap(~variable, scales = "free_y", ncol = 2, strip.position = "left") +
theme(strip.background = element_blank(), strip.placement = "outside")
据我所知,在 vanilla ggplot2 中没有办法做到这一点。如果您对 gtables 很满意table,您可能会发现以下方法可行。
library(ggplot2)
# Base plot
p <- ggplot(economics_long, aes(date, value)) +
geom_line() +
labs(y="") +
theme(strip.background = element_blank(), strip.placement = "outside")
# Left aligned strips/axes
left <- p +
facet_wrap(~variable, scales = "free_y", ncol = 2, strip.position = "left")
# Right aligned strips/axes
right <- p +
facet_wrap(~variable, scales = "free_y", ncol = 2, strip.position = "right") +
scale_y_continuous(position = "right")
# Convert to gtables
left <- ggplotGrob(left)
right <- ggplotGrob(right)
# Combine parts of left and right
# Column numbers found by browsing through layout
gt <- cbind(left[, 1:7], right[, 9:ncol(right)])
# Render
grid::grid.newpage(); grid::grid.draw(gt)
由 reprex package (v2.0.1)
于 2021-10-20 创建要以比手动判断 table 布局更编程的方式找到面板位置,我最好的猜测是这样做:
panels_left <- panel_cols(left)$l
panels_right <- panel_cols(right)$l
# The -2 is to include the left axis space (zero width because of absence)
# and the panel spacing
gt <- cbind(left[, 1:panels_left[1]],
right[, (panels_right[2]-2):ncol(right)])
grid::grid.newpage(); grid::grid.draw(gt)