我正在嘗試添加一個表格,將我在 facet grid boxplot 中的資訊匯總到同一個圖中。
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
我已經嘗試了幾個使用plotrixand的解決方案ggpmisc,但沒有運氣。理想情況下,該表將代替添加到網格中的另一列。想知道這是否最簡單,gridExtra并將它們作為單獨的物件添加在一起?任何幫助將不勝感激。
uj5u.com熱心網友回復:
你可以嘗試拼湊和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))

由
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471584.html
