【问题标题】:Automating multiple plots/graphs with two Ys from one data set使用来自一个数据集的两个 Y 自动化多个绘图/图形
【发布时间】:2019-10-08 01:53:22
【问题描述】:

我正在尝试使用两个 Y 变量绘制 34 个单独的图与时间的关系。我的数据集是结构化的: X Y1A Y2A Y1B Y2B 。 . . Y1n Y2n

我可以通过简单地要求 ggplot 绘制我想要的列来单独绘制。但是,我在 5 个数据集中的每个数据集中都有 34 个绘图要做,因此自动化这将很有用。我真的很困惑如何构建语言来做我想做的事情。 (新用户)。我不擅长 for 循环,甚至不知道这是否正是我想要使用的正确代码结构。我查看了 apply 函数,但这些函数似乎并没有做我想做的事情,因为我不想操纵我的数据,而只是一次绘制所有数据。

我还没有尝试过melt,但我再次被困在如何一次创建两条线。我是否应该将我的列命名为不同的/更易于自动化的名称。

我已经搜索了几个小时,但没有找到任何可以完全回答我的问题的东西。


df <- matrix( c( 1,   0.011,    0.002,     0.007,      0.002,
              2,   0.011,      NA,   0.007, NA,
              3,   0.010,    0.002,     0.007,      0.002,
              4,   0.010,      NA,       0.007, NA, 
              5,   0.010,       NA,      0.006, NA, 
              6,   0.010,    0.002,    0.006,       0.002
              ), nrow = 6, ncol= 5, byrow= TRUE) 
colnames(df)<- c("time", "prefish1",  "obsfish1",  "prefish2",  "obsfish2")
biomass<-as.data.frame(df)
for (i in biomass[2]) {   
}
for (k in biomass[3]) {

}
}  ##this will call just columns 2 and 3 as a pair.##
ggplot(biomass, aes(x= time)) +
  geom_line(aes(y=i)) +
  geom_point(aes(y=k)) +
  ggtitle(i) + ylab("biomass t/km2") + 
  ##I would also like to title them using the header one of the column pairs## 
  theme_classic()

编辑 我刚试过这个:

pre<- biomass[+c(2,4)] 
obs<- biomass[+c(3,5)] 
for(i in 1:NCOL(pre)){
}
  for(k in 1:NCOL(obs)){
    plot(biomass$time, pre[,i], type= "l", xlab= "time", ylab ="biomass t/km2")
    par(new= TRUE)
    plot(biomass$time, obs[,k], type = "p", main = paste("Fit", names(obs)[k]), xlab= "time", ylab ="biomass t/km2")

  }

但是这里出了点问题,我希望每一对的点和线都在同一个图上。

预期结果应该是 34 个单独的地块。 - 或者在这个例子中,两个单独的图。

【问题讨论】:

  • 请使用dput(your_data)添加可重现的数据示例。

标签: r for-loop ggplot2


【解决方案1】:

另一个选项:构建一个绘图函数。然后创建两个名称向量,同时循环它们,传递给新创建的函数并将结果保存在一个列表中。之后使用plot_grid 将所有地块组合在一起

library(tidyverse)

## plot function
my_bio_plot <- function(biomass, .var1, .var2) {

  p <- ggplot(biomass, aes(x = time)) +
    geom_line(aes(y = .data[[.var1]])) +
    geom_point(aes(y = .data[[.var2]])) +
    ggtitle(paste0(.var1, " (line) vs ", .var2, " (dot)")) + 
    ylab(expression("biomas"~(t/km^2))) +
    theme_classic()
  return(p)
}

## create two separate name vectors
var1_names <- colnames(biomass)[grepl("^pre", colnames(biomass))]
var2_names <- colnames(biomass)[grepl("^obs", colnames(biomass))]

## loop through two vectors simultaneously and save result in a list 
# ..1 = var1_names, ..2 = var2_names
my_plot <- pmap(list(var1_names, var2_names), ~ my_bio_plot(biomass, ..1, ..2))

## merge plots together
# https://cran.r-project.org/web/packages/cowplot/
# install.packages("cowplot", dependencies = TRUE)
library(cowplot)

plot_grid(plotlist = my_plot,
          ncol = 2,
          labels = "AUTO")

reprex package (v0.3.0) 于 2019-05-21 创建

【讨论】:

  • 我通过调用一个新的绘图窗口让它工作了!谢谢!
  • 或者你可以使用ggsave。如果您在 1 个单页中有太多情节,save them to multiple pages
【解决方案2】:

分面方法是否可行,或者您想要单独的输出文件?

在 ggplot 中执行此操作的最常见方法是将单独的运行放入构面中。为此,我们需要在将数据输入 ggplot 之前对其进行整形:不是在每个图表的数据中都有单独的列对,我们可以有一对列,另一列(下面的num)指定哪个图表每一行都涉及到。然后我们可以使用num 变量映射到构面。 (我们也可以使用相同的准备来完成第二个任务......)

library(tidyr)   # Useful for reshaping the data
library(ggplot2)

biomass %>%
  # Gather data into long format
  gather(column, value, -time) %>%
  # Separate the number of graph from the type of column
  separate(column, c("var", "num"), sep = -1) %>%
  # Put the two types of column back into separate columns
  spread(var, value) %>%


  ggplot(aes(x= time, group = num)) +
  geom_line(aes(y = prefish)) +
  geom_point(aes(y= obsfish)) +
  facet_wrap(~num) +
  ylab("biomass t/km2") + 
  ##I would also like to title them using the header one of the column pairs## 
  theme_classic()

【讨论】:

猜你喜欢
  • 2013-12-31
  • 2022-07-07
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
相关资源
最近更新 更多