strip.position external in 两列 facet_wrap 图

Posted

技术标签:

【中文标题】strip.position external in 两列 facet_wrap 图【英文标题】:strip.position external in two columns facet_wrap plot 【发布时间】:2021-12-07 07:28:24 【问题描述】:

在两列 facet_wrap 图形中,将 facet 的名称和轴放在左侧用于左列,在右侧用于右列,这样会更方便和更好,以便获得紧凑的绘图,刻面靠得很近。

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")

【问题讨论】:

【参考方案1】:

据我所知,在香草 ggplot2 中没有办法做到这一点。如果您对 gtables 感到满意,您可能会发现以下方法可行。

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 创建

要以编程方式而不是手动判断表格布局来查找面板位置,我最好的猜测是这样做:

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)

【讨论】:

以上是关于strip.position external in 两列 facet_wrap 图的主要内容,如果未能解决你的问题,请参考以下文章

PHP解析xml文件是报错:I/O warning : failed to load external entity

为什么在函数内初始化extern变量会产生错误?

C语言中extern的意思是啥?

c语言中的extern是啥,有啥作用啊?

c++程序错误error LNK2001: unresolved external symbol "public: void __thiscall CEmp::print(void)&quo

求大神详细解答c语言extern的连接问题 我这里有两个程序 想使用extern来连接具体该怎么用?