R中具有ggtext和gridtext的R中具有跨图图例的多面板图

菲金斯

我将通过下面的跨越textgrob的图例接近实现此多面板图。但是,在人物和人物图例之间我仍然得到了意想不到的空间。尝试以下reprex。

# Library calls
library(tidyverse)
library(grid)
library(gridtext)
library(ggtext)
library(patchwork)

# make dummy figures
d1 <- runif(500)
d2 <- rep(c("Treatment", "Control"), each=250)
d3 <- rbeta(500, shape1=100, shape2=3)
d4 <- d3 + rnorm(500, mean=0, sd=0.1)
plotData <- data.frame(d1, d2, d3, d4)
str(plotData)
#> 'data.frame':    500 obs. of  4 variables:
#>  $ d1: num  0.0177 0.2228 0.5643 0.4036 0.329 ...
#>  $ d2: Factor w/ 2 levels "Control","Treatment": 2 2 2 2 2 2 2 2 2 2 ...
#>  $ d3: num  0.986 0.965 0.983 0.979 0.99 ...
#>  $ d4: num  0.876 0.816 1.066 0.95 0.982 ...

p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4)) +
  theme(plot.background = element_rect(color='black'))
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2, y=d1, fill=d2))+
  theme(legend.position="none") +
  theme(plot.background = element_rect(color='black'))
p3 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d1, color=I("black"), fill=I("orchid"))) +
  theme(plot.background = element_rect(color='black'))
p4 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d3, color=I("black"), fill=I("goldenrod"))) +
  theme(plot.background = element_rect(color='black'))


fig_legend <- textbox_grob(
  "**Figure 1.**  Testing Control vs. Treatment.   A. Scatterplot. 
  B. The outcomes in the control arm were significantly better than 
  the Treatment Arm. C. Histogram. D. Another Histogram.",
  gp = gpar(fontsize = 11),
  box_gp = gpar(col = "black",   linetype = 1),
  padding = unit(c(3, 3, 3, 3), "pt"),
  margin = unit(c(0,0,0,0), "pt"),
  height = unit(0.6, "in"),
  width = unit(1, "npc"),
  #x = unit(0.5, "npc"), y = unit(0.7, "npc"),
  r = unit(0, "pt")
)


p1 + {
  p2 + {
    p3 +
      p4 +
      plot_layout(ncol=1)
  }
} + fig_legend +
  plot_layout(ncol=1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex软件包(v0.3.0)创建于2020-02-09

克劳斯·威尔克

正确的方法是使用plot_annotation()字幕两侧各有一些水平间隙的原因是,与常规ggplot2一样,绘图边距仍应用于字幕。如果要避免这种情况,则必须将绘图边距设置为0,并通过在轴标题等中添加适当的边距来创建间距。

# Library calls
library(tidyverse)
library(ggtext)
library(patchwork)

# make dummy figures
d1 <- runif(500)
d2 <- rep(c("Treatment", "Control"), each=250)
d3 <- rbeta(500, shape1=100, shape2=3)
d4 <- d3 + rnorm(500, mean=0, sd=0.1)
plotData <- data.frame(d1, d2, d3, d4)

p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4)) +
  theme(plot.background = element_rect(color='black'))
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2, y=d1, fill=d2))+
  theme(legend.position="none") +
  theme(plot.background = element_rect(color='black'))
p3 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d1, color=I("black"), fill=I("orchid"))) +
  theme(plot.background = element_rect(color='black'))
p4 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d3, color=I("black"), fill=I("goldenrod"))) +
  theme(plot.background = element_rect(color='black'))


fig_legend <- plot_annotation(
  caption = "**Figure 1.**  Testing Control vs. Treatment.   A. Scatterplot. 
  B. The outcomes in the control arm were significantly better than 
  the Treatment Arm. C. Histogram. D. Another Histogram.",
  theme = theme(
    plot.caption = element_textbox_simple(
      size = 11,
      box.colour = "black",
      linetype = 1,
      padding = unit(c(3, 3, 3, 3), "pt"),
      r = unit(0, "pt")
    )
  )
)


p1 + {
  p2 + {
    p3 +
      p4 +
      plot_layout(ncol=1)
  }
} + fig_legend +
  plot_layout(ncol=1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex软件包(v0.3.0)创建于2020-02-09

实际上,您可以在标题上使用负边距来抵消绘图边距。

fig_legend <- plot_annotation(
  caption = "**Figure 1.**  Testing Control vs. Treatment.   A. Scatterplot. 
  B. The outcomes in the control arm were significantly better than 
  the Treatment Arm. C. Histogram. D. Another Histogram.",
  theme = theme(
    plot.caption = element_textbox_simple(
      size = 11,
      box.colour = "black",
      linetype = 1,
      padding = unit(c(3, 3, 3, 3), "pt"),
      margin = unit(c(0, -5.5, 0, -5.5), "pt"),
      r = unit(0, "pt")
    )
  )
)


p1 + {
  p2 + {
    p3 +
      p4 +
      plot_layout(ncol=1)
  }
} + fig_legend +
  plot_layout(ncol=1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex软件包(v0.3.0)创建于2020-02-09

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

生成具有交替轴和不同比例的堆叠式多面板图

来自分类Dev

如何在 R ggplot 中创建具有不同离散 X 轴的多面条形图

来自分类Dev

具有数字x轴的R中的字母图

来自分类Dev

R中具有多列的条形图

来自分类Dev

R中具有汇总数据的条形图

来自分类Dev

R中具有多列的堆积条形图

来自分类Dev

R ggplot中具有可变颜色的多个图

来自分类Dev

R中具有连接的两侧bean图

来自分类Dev

ggraph R 中节点具有特定比例的线性图

来自分类Dev

R中具有堆叠条和簇条的水平条形图

来自分类Dev

R中具有分类变量和系统发生树的热图

来自分类Dev

R中带有图例a列的条形图

来自分类Dev

具有某些变量的R-图

来自分类Dev

具有不同模态的R图

来自分类Dev

具有内部图例大小的带有和不带有图例的两个图

来自分类Dev

具有内部图例大小的带有和不带有图例的两个图

来自分类Dev

R ggplot热图,其中多行在同一图形上具有单独的图例

来自分类Dev

R:多面条形图,每个标签均具有独立的百分比标签

来自分类Dev

具有Seaborn的PairGrid中的Hexbin图

来自分类Dev

如何在R中的x和y轴上绘制具有置信区间的二元图?

来自分类Dev

创建由R中的两个分组变量分组的数据集的多面板图

来自分类Dev

使用 R 中的 openair 包更改多面板泰勒图的字体大小

来自分类Dev

Seaborn / Matplotlib:具有图例中不同变量的色相(像热图)的散点图?

来自分类Dev

对齐多面图和图例

来自分类Dev

将文本对齐到R中具有可变大小的图

来自分类Dev

可视化R / igraph中具有3层(三方)的图/网络

来自分类Dev

在R-markdown中创建具有光泽的动态图

来自分类Dev

R中具有置信区间图的线性回归预测

来自分类Dev

如何为R中具有多个图层的条形图设置颜色?

Related 相关文章

  1. 1

    生成具有交替轴和不同比例的堆叠式多面板图

  2. 2

    如何在 R ggplot 中创建具有不同离散 X 轴的多面条形图

  3. 3

    具有数字x轴的R中的字母图

  4. 4

    R中具有多列的条形图

  5. 5

    R中具有汇总数据的条形图

  6. 6

    R中具有多列的堆积条形图

  7. 7

    R ggplot中具有可变颜色的多个图

  8. 8

    R中具有连接的两侧bean图

  9. 9

    ggraph R 中节点具有特定比例的线性图

  10. 10

    R中具有堆叠条和簇条的水平条形图

  11. 11

    R中具有分类变量和系统发生树的热图

  12. 12

    R中带有图例a列的条形图

  13. 13

    具有某些变量的R-图

  14. 14

    具有不同模态的R图

  15. 15

    具有内部图例大小的带有和不带有图例的两个图

  16. 16

    具有内部图例大小的带有和不带有图例的两个图

  17. 17

    R ggplot热图,其中多行在同一图形上具有单独的图例

  18. 18

    R:多面条形图,每个标签均具有独立的百分比标签

  19. 19

    具有Seaborn的PairGrid中的Hexbin图

  20. 20

    如何在R中的x和y轴上绘制具有置信区间的二元图?

  21. 21

    创建由R中的两个分组变量分组的数据集的多面板图

  22. 22

    使用 R 中的 openair 包更改多面板泰勒图的字体大小

  23. 23

    Seaborn / Matplotlib:具有图例中不同变量的色相(像热图)的散点图?

  24. 24

    对齐多面图和图例

  25. 25

    将文本对齐到R中具有可变大小的图

  26. 26

    可视化R / igraph中具有3层(三方)的图/网络

  27. 27

    在R-markdown中创建具有光泽的动态图

  28. 28

    R中具有置信区间图的线性回归预测

  29. 29

    如何为R中具有多个图层的条形图设置颜色?

热门标签

归档