在 R 的数据帧中使用循环使用选定的信息信息绘制多个绘图

Using loops to make multiple plots with selected information information in dataframe in R

提问人:K.W 提问时间:11/9/2023 最后编辑:Allan CameronK.W 更新时间:11/9/2023 访问量:51

问:

我有一个看起来像这样的数据帧,我想使用 for 循环来制作 y 轴上有生存期和 x 轴上时间的图,每条线、性别和剂量组合。例如,我想要一个第 1 行第 138 行女性的生存与时间图,然后为第 1 剂第 138 行的男性绘制另一个图,依此类推。我该怎么做?

时间 生存 线 剂量
28 0.95 138 F 1
35 0.65 138 F 1
41 0.6 138 F 1
42 0.15 138 F 1
48 0.05 138 F 1
55 0 138 F 1
5 0.95 138 F 1
11 0.855 138 F 1
28 0.95 138 M 1
35 0.65 138 M 1
41 0.6 138 M 1
42 0.15 138 M 1
48 0.05 138 M 1
55 0 138 M 1
5 0.95 138 M 1
11 0.855 138 M 1
28 0.95 120 F 2
35 0.65 120 F 2
41 0.6 120 F 2
42 0.15 120 F 2
48 0.05 120 F 2
55 0 120 F 2
5 0.95 120 F 2
11 0.855 120 F 2
28 0.95 120 M 2
35 0.65 120 M 2
41 0.6 120 M 2
42 0.15 120 M 2
48 0.05 120 M 2
55 0 120 M 2
5 0.95 120 M 2
11 0.855 120 M 2
R DataFrame for 循环

评论

0赞 I_O 11/9/2023
当前的几个图形包(ggplot2、plotly、highcharter、lattice ...)具有专用功能,用于按类别分面绘图。例如,见facet_wrap

答:

0赞 paulbouu 11/9/2023 #1

这应该有效,假设您的数据框已命名并且:dfclass(df) == "data.frame"

library(ggplot2)

combinations <- unique(df[, c("Line", "Sex", "Dose")])

for (i in 1:nrow(combinations)) {
  line <- combinations$Line[i]
  sex <- combinations$Sex[i]
  dose <- combinations$Dose[i]

  subdata <- df[df$Line == line & df$Sex == sex & df$Dose == dose, ]
  
  plot <- ggplot(subdata, aes(x = Time, y = Survival)) + geom_line() + labs(title = paste("Line", line, ", Sex", sex, ", Dose", dose),
         x = "Time", y = "Survival")
  print(plot)
}

不过,您提供的数据会绘制相同的图。

评论

0赞 K.W 11/9/2023
感谢您的帮助。此外,如果我想在 y 轴上绘制生存期,并在 x 轴上绘制每个性别和线组合的一个时间点(假设时间=11)的剂量,代码将如何更改?
0赞 paulbouu 11/9/2023
你可以像这样过滤你的数据框,组合会稍微改变一点:。然后在代码的下一部分,只需更改轴即可获得所需的内容df_filtered <- df[df$Time == 11, ]combinations <- unique(df_filtered[, c("Line", "Sex")])
0赞 Allan Cameron 11/9/2023 #2

这很简单,使用 ,没有任何循环。ggplot

library(ggplot2)

ggplot(df, aes(Time, Survival)) +
  geom_line() +
  facet_grid(paste('Sex:', Sex) ~ paste('Line', Line) + paste('Dose', Dose)) +
  theme_bw()

enter image description here

0赞 jay.sf 11/9/2023 #3

在 中使用。plotby

> par(mfrow=with(dat, table(Sex, Dose)) |> dim())
> transform(dat, fac=Reduce(paste0, data.frame(Sex, Dose))) |>
+   by(~fac, \(x) plot(Survival ~ Time, x, type='s', main=el(fac)))

enter image description here

如果我们有太多的组合,我们需要一种不同的方法。par


数据:

> dput(dat)
structure(list(Time = c(28L, 35L, 41L, 42L, 48L, 55L, 5L, 11L, 
28L, 35L, 41L, 42L, 48L, 55L, 5L, 11L, 28L, 35L, 41L, 42L, 48L, 
55L, 5L, 11L, 28L, 35L, 41L, 42L, 48L, 55L), Survival = c(0.95, 
0.65, 0.6, 0.15, 0.05, 0, 0.95, 0.855, 0.95, 0.65, 0.6, 0.15, 
0.05, 0, 0.95, 0.855, 0.95, 0.65, 0.6, 0.15, 0.05, 0, 0.95, 0.855, 
0.95, 0.65, 0.6, 0.15, 0.05, 0), Line = c(138L, 138L, 138L, 138L, 
138L, 138L, 138L, 138L, 138L, 138L, 138L, 138L, 138L, 138L, 138L, 
138L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 
120L, 120L, 120L, 120L), Sex = c("F", "F", "F", "F", "F", "F", 
"M", "M", "M", "M", "M", "M", "M", "M", "F", "F", "F", "F", "F", 
"F", "F", "F", "M", "M", "M", "M", "M", "M", "M", "M"), Dose = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), class = "data.frame", row.names = c(NA, 
-30L))

评论

0赞 K.W 11/10/2023
非常感谢。我也遇到了以下问题:如果我想浏览我的数据帧并将每个“剂量”在指定时间(假设时间 =11)的每个“剂量”的“生存”保存到单独的数据帧中,我如何使用 for 循环?
0赞 jay.sf 11/10/2023
@K.W 你可能想要.不过,您应该将其作为新问题来问。subset(dat, Time == 11)