繁体   English   中英

使用 for 循环重新命名绘图列表

[英]Re-title list of plots with for loop

我已经使用 ggplot2 生成了直方图。我想将这些图重命名为创建它们后的最后一步。 我正在考虑使用 for 循环,但无法让它正常工作。

这些图包含在 class "list"的变量hist中,我通过键入以下内容来访问它们:

hist$A
hist$B
hist$C

## rename plots as the last step 

name <- c("A", "B", "C")

for (i in seq_along(hist)) {
  hist[[i]] +
    labs(title = name[[i]])
}

# this code works but I can't replicate it in a for loop
hist$C +
  labs(
    title = name[3])

我希望hist$B的标题是"B"

您需要分配回hist内的项目:

name <- c("A", "B", "C")

for (i in seq_along(hist)) {
  hist[i] <- hist[[i]] +
    labs(title = name[i])
}

并且不要忘记,为了在图形设备上生成 output,您需要print一个grid图形 object,这是 ggplot 项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM