我试图添加一个表格,将我在刻面网格盒形图中的信息汇总到同一个图中.

df<- structure(list(design = c("F", "F", "F", "F", "H", "H", "H", 
"H", "H", "R", "R", "R", "R", "R", "SR", "SR", "SR", "SR", "SR"
), Species = c("LMB", "LMB", "BLG", "BLG", "BLG", "BLG", "LMB", 
"BLG", "LMB", "BLG", "LMB", "LMB", "LMB", "BLG", "BLG", "LMB", 
"BLG", "BLG", "LMB"), Value = c(10.9673144080207, 9.69716158252908, 
13.6176455626267, 12.5184221861068, 7.07161082124456, 26.6081334130079, 
4.83476200538245, -2.44881629932982, 1.63297655884495, 18.8302775817964, 
6.8889811208883, 4.6387733793561, 7.56071784901834, -26.3109417867886, 
27.4553930399756, -3.19241479249914, -9.65440732501407, 40.3505935270246, 
2.9681096231288), season = structure(c(1L, 2L, 1L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Spring", 
"Fall"), class = "factor")), row.names = c(NA, -19L), class = c("data.table", 
"data.frame"))

ggplot(df, aes(x=design, y=Value))+
  geom_boxplot()+
  stat_summary(fun = mean, shape=21, size=1, fill='red', col='red', geom='point')+
  #geom_jitter(alpha = 0.09, width = 0.2)+
  facet_grid(season ~ Species)+
  ylab("Relative Bias (RB%)")+
  xlab("Design")+
   theme_light()

我想将以下各观测值的平均值总和添加到一个表格中,并添加到绘图的右侧:

table<- structure(list(indicator = c("total_spring", "total_fall"), season = c("Spring", 
"Fall"), f = c("22", "15"), h = c("1.9", "23.1"), r = c("-6.1", 
"13"), sr = c("9", "-8")), class = "data.frame", row.names = c(NA, 
-2L)) # numbers are made up for example, not representative of data in boxplots

我用plotrixggpmisctry 了几种解决方案,但没有成功.理想情况下,此表将取代添加到网格中的另一列.想知道使用gridExtra并将它们作为单独的对象添加在一起是否最简单?任何帮助都将不胜感激.

推荐答案

你可以try 拼凑和gridExtra:


library(ggplot2)
library(patchwork)
library(gridExtra)

gg <- ggplot(df, aes(x=design, y=Value))+
  geom_boxplot()+
  stat_summary(fun = mean, shape=21, size=1, fill='red', col='red', geom='point')+
  facet_grid(season ~ Species)+
  ylab("Relative Bias (RB%)")+
  xlab("Design")+
  theme_light()

# use gridExtra to turn the table into a Grob
table <- tableGrob(table)

# plot side by side with patchwork, control relative size with `widths` and `heights` arguments
gg + table +
  plot_layout(widths = c(5, 7),
              heights = c(5, 3))

reprex package(v2.0.1)于2022-05-06创建

R相关问答推荐

如何创建构成多个独立列条目列表的收件箱框列?

从gtsummary包中使用tBL_strata()和tBL_summary()时删除变量标签

如何对数据集进行逆向工程?

获取一个数据库框架的摘要,该数据库框架将包含一列数据库框架,

derrr mutate case_when grepl不能在R中正确返回值

如果某些列全部为NA,则更改列

在嵌套列表中查找元素路径的最佳方法

您是否可以折叠R中的重复行,同时保留基于所选列的值?

按组计算列中1出现的间隔年数

R中的类别比较

如何在ggplot2中创建多个y轴(每个变量一个)

在R中使用列表(作为tibble列)进行向量化?

将统计检验添加到GGPUBR中的盒图,在R

根据r中每行中的日期序列,使用列名序列创建新列

我需要使用ggplot2制作堆叠条形图

注释不会绘制在所有ggplot2面上

如何在shiny 的应用程序 map 视图宣传单中可视化单点

如何创建直方图与对齐的每月箱?

臭虫?GradeThis::grade_this_code()在`-code-check`块中失败

R中刻面网格中的排序条形图