我正在尝试在另一个函数中使用walk
函数。我的图表右侧有一张表格,我希望它根据Site ID变量的值进行更改。
下面是一个我希望使用mytable
作为占位符的表格的示例。但是,对于我的实际用例,我希望这些值变化Site_ID
:
Site_ID
=“ H1 ”:Alpha1
应该是 0.008 ,n_ho
应该是 1.09 ,而A
应该是 5.45 ;
Site_ID
=“ H2 ”:Alpha1
应该是 0.125 ,n_ho
应该是 2.28 ,而A
应该是 2.78 。
等。
我的数据:
> dput(Infil_Data2)
structure(list(
Time = c(0L, 30L, 60L, 90L, 120L, 150L, 180L,
210L, 240L, 270L, 300L, 0L, 30L, 60L, 90L, 120L, 150L, 180L,
210L, 240L, 270L, 300L, 0L, 30L, 60L, 90L, 120L, 150L, 180L,
210L, 240L, 270L, 300L),
Site_ID = c("H1", "H1", "H1", "H1",
"H1", "H1", "H1", "H1", "H1", "H1", "H1", "H2", "H2", "H2", "H2",
"H2", "H2", "H2", "H2", "H2", "H2", "H2", "H3", "H3", "H3", "H3",
"H3", "H3", "H3", "H3", "H3", "H3", "H3"),
Vol_mL = c(63, 62,
60, 59, 58, 56, 54, 52.5, 50, 48.5, 46.5, 82, 77, 73, 68, 65,
51, 56, 52, 47.5, 42.5, 37.5, 69, 67, 65, 63, 61, 60, 58, 56,
54, 51.5, 49),
Soil_Type = c("Clay", "Clay", "Clay", "Clay",
"Clay", "Clay", "Clay", "Clay", "Clay", "Clay", "Clay", "Loamy Sand",
"Loamy Sand", "Loamy Sand", "Loamy Sand", "Loamy Sand", "Loamy Sand",
"Loamy Sand", "Loamy Sand", "Loamy Sand", "Loamy Sand", "Loamy Sand",
"Sandy Loam", "Sandy Loam", "Sandy Loam", "Sandy Loam", "Sandy Loam",
"Sandy Loam", "Sandy Loam", "Sandy Loam", "Sandy Loam", "Sandy Loam",
"Sandy Loam"),
Radius = c(1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6,
1.6, 1.6, 1.6, 1.6, 2.25, 2.25, 2.25, 2.25, 2.25, 2.25, 2.25,
2.25, 2.25, 2.25, 2.25, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6,
1.6, 1.6, 1.6),
Suction = c("X.0.5", "X.0.5", "X.0.5", "X.0.5",
"X.0.5", "X.0.5", "X.0.5", "X.0.5", "X.0.5", "X.0.5", "X.0.5",
"X.1", "X.1", "X.1", "X.1", "X.1", "X.1", "X.1", "X.1", "X.1",
"X.1", "X.1", "X.2", "X.2", "X.2", "X.2", "X.2", "X.2", "X.2",
"X.2", "X.2", "X.2", "X.2"),
Sqrt_Time.x = c(0, 5.477225575,
7.745966692, 9.486832981, 10.95445115, 12.24744871, 13.41640786,
14.49137675, 15.49193338, 16.43167673, 17.32050808, 0, 5.477225575,
7.745966692, 9.486832981, 10.95445115, 12.24744871, 13.41640786,
14.49137675, 15.49193338, 16.43167673, 17.32050808, 0, 5.477225575,
7.745966692, 9.486832981, 10.95445115, 12.24744871, 13.41640786,
14.49137675, 15.49193338, 16.43167673, 17.32050808),
Cal_Vol_cm = c(0,
0.124339799, 0.373019398, 0.497359197, 0.621698996, 0.870378595,
1.119058194, 1.305567893, 1.616417391, 1.80292709, 2.051606688,
0, 0.621698996, 1.119058194, 1.74075719, 2.113776588, 3.854533778,
3.232834782, 3.730193979, 4.289723076, 4.911422072, 5.533121068,
0, 0.248679599, 0.497359197, 0.746038796, 0.994718394, 1.119058194,
1.367737792, 1.616417391, 1.865096989, 2.175946488, 2.486795986),
X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA),
X.1 = c(3.141592654, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
Alpha1 = c("0.008",
"0.008", "0.008", "0.008", "0.008", "0.008", "0.008", "0.008",
"0.008", "0.008", "0.008", "0.124", "0.124", "0.124", "0.124",
"0.124", "0.124", "0.124", "0.124", "0.124", "0.124", "0.124",
"0.075", "0.075", "0.075", "0.075", "0.075", "0.075", "0.075",
"0.075", "0.075", "0.075", "0.075"),
n_ho = c("1.09", "1.09",
"1.09", "1.09", "1.09", "1.09", "1.09", "1.09", "1.09", "1.09",
"1.09", "2.28", "2.28", "2.28", "2.28", "2.28", "2.28", "2.28",
"2.28", "2.28", "2.28", "2.28", "1.89", "1.89", "1.89", "1.89",
"1.89", "1.89", "1.89", "1.89", "1.89", "1.89", "1.89"),
A = c(5.452375501,
5.452375501, 5.452375501, 5.452375501, 5.452375501, 5.452375501,
5.452375501, 5.452375501, 5.452375501, 5.452375501, 5.452375501,
2.786831249, 2.786831249, 2.786831249, 2.786831249, 2.786831249,
2.786831249, 2.786831249, 2.786831249, 2.786831249, 2.786831249,
2.786831249, 5.332170741, 5.332170741, 5.332170741, 5.332170741,
5.332170741, 5.332170741, 5.332170741, 5.332170741, 5.332170741,
5.332170741, 5.332170741)),
row.names = c(NA, -33L), class = "data.frame")
我的代码:
library(dplyr)
library(purrr)
library(ggplot2)
library(ggpmisc)
library(gridExtra)
mytable <- cbind(c("Alpha1", "n_ho", "A"), c(1, 2, 3))
plot_2 <-
Infil_Data2 %>%
split(.$Site_ID) %>%
map2(names(.), ~ggplot(.x, aes(Sqrt_Time.x, Cal_Vol_cm)) +
geom_point() +
labs(title = paste(.y)) +
theme(plot.title = element_text(hjust = 0.5)) +
stat_smooth(mapping = aes(x = Sqrt_Time.x, y = Cal_Vol_cm),
method = "lm", se = FALSE,
formula = y ~ poly(x, 2, raw = TRUE), color = "red") +
theme(plot.margin = unit(c(1, 5, 1, 1), "cm")) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
label.x.npc = "left", label.y.npc = 0.90, #set the position of the eq
formula = y ~ poly(x, 2, raw = TRUE), parse = TRUE, rr.digits = 3) +
annotation_custom(tableGrob(mytable, rows = NULL),
xmin = unit(20,"npc"), xmax = unit(25,"npc"),
ymin = 0.05, ymax = 0.1))
pdf("allplots_2.pdf", onefile = TRUE)
walk(plot_2, print)
dev.off()
答案 0 :(得分:0)
这是您要寻找的吗?
plot_2 <-
Infil_Data2 %>%
split(.$Site_ID) %>%
map2(names(.), ~ggplot(.x, aes(Sqrt_Time.x, Cal_Vol_cm)) +
geom_point() +
labs(title = paste(.y)) +
theme(plot.margin = unit(c(1, 5, 1, 1), "cm")) +
annotation_custom(tableGrob(cbind(c("Alpha1", "n_ho", "A"),
c(.x$Alpha1[1],
.x$n_ho[1],
.x$A[1])),
rows = NULL),
xmin = unit(20, "npc"), xmax = unit(25, "npc"),
ymin = 0.05, ymax = 0.1))
(我已经排除了一些关于几何图层/地块外观其他方面的代码,因为我认为它们对于所提出的问题并不重要。)