【问题标题】:Loop printing lots of graphs in order (PDF) using ggplot2 in R使用 R 中的 ggplot2 按顺序循环打印大量图形 (PDF)
【发布时间】:2020-07-11 20:24:14
【问题描述】:

由于贝叶斯逻辑回归,我有一个大型数据集。数据集包含参数估计、置信区间等(见下文)。

        mean         sd confint_2.5 confint_97.5     Rhat     median    spec    Errorup Errordown
1 -0.7897597 0.18668304  -1.1759960   -0.4517294 1.002211 -0.7811156 Marvulg -0.3293862 -1.957112
2 -0.7891327 0.08145761  -0.9570086   -0.6380287 1.000155 -0.7861764 Viotric -0.1481477 -1.743185
3 -0.6619662 0.26049168  -1.2203315   -0.2059030 1.045208 -0.6440501 Antdioi -0.4381470 -1.864382
4 -0.6571516 0.17940842  -1.0417642   -0.3364415 1.008100 -0.6470382 Eleacic -0.3105968 -1.688802
5 -0.6526717 0.20005184  -1.0816375   -0.2968111 1.005126 -0.6394952 Antcotu -0.3426842 -1.721133
6 -0.6497648 0.16620699  -1.0081607   -0.3555847 1.003738 -0.6384035 Triflav -0.2828188 -1.646564 

我总共有 714 行数据,从低到高排序(平均)。我使用此代码一次绘制 50 个,其中 a3_sort 是 50 行数据的子集(因此手动执行 a3_sort <- a3[n:n,), 之后我打印子集并继续下一个 50):

ggplot2::ggplot(data = a3_sort, mapping = aes(x = reorder(spec, mean), y = mean, ymin = confint_97.5, ymax = confint_2.5))+
  geom_pointrange()+
  geom_hline(yintercept = 0, lty = 2)+
  coord_flip()+
  xlab ("species") +ylab ("mean (credibility interval)")+
  theme_bw()

这行得通,我得到了我想要的,但必须有一种更少体力劳动的方法来做到这一点?

我的问题:有没有办法循环这个过程,自动将 PDF 保存在工作目录中?

下面是一个情节的示例:

【问题讨论】:

  • 那么您是否想要为从原始数据中获得的每 50 行数据帧绘制一个图?
  • 是的,完全正确。所以总共 15 个地块,无需手动子集和保存地块。
  • 我已经为您的问题添加了一个可能的解决方案。请检查并让我知道它是否有效:)

标签: r loops ggplot2


【解决方案1】:

您可以尝试此解决方案。我用虚拟数据DF 测试了714 行和与您相同的列。 DF 在您的情况下是您的 714 行的排序数据框和您拥有的变量。我已经设置了代码,如果您需要大于 50 的宽度,您可以更改。

library(zoo)
#Create keys; change 50 if you want a larger window
keys <- seq(1, nrow(DF), 50)
vals=1:length(keys)
#Flag to allocate the position and values
#na.locf is used to complete NA so that we have same index
DF$Flag <- NA
DF$Flag[keys]<-vals
DF$Flag <- na.locf(DF$Flag)
#Then split by flag
ListData <- split(DF,DF$Flag)
#Function to create plot
myplot <- function(x)
{
  tplot <- ggplot2::ggplot(data = x, mapping = aes(x = reorder(spec, mean), y = mean, ymin = confint_97.5, ymax = confint_2.5))+
    geom_pointrange()+
    geom_hline(yintercept = 0, lty = 2)+
    coord_flip()+
    xlab ("species") +ylab ("mean (credibility interval)")+
    theme_bw()
  return(tplot)
}
#Replicate plots
LPlots <- lapply(ListData,myplot)
#Export to pdf
pdf('Myplots.pdf',width = 14)
for(i in c(1:length(LPlots)))
{
  plot(LPlots[[i]])
}
dev.off()
 

最后,您将获得 pdf 格式的绘图。我希望这有帮助。如果您有任何疑问,请告诉我。

【讨论】:

  • 嗨鸭子,感谢您的回复!不幸的是,这对我有用!我做了一些调整(例如,对于不同的绘图保持轴长度相同)。感谢您的代码!
【解决方案2】:

这种方法可以适应您的情况:

# Some dummy data:

df <- data.frame(g = letters[1:24],
                 min = sample(0:10, 24, replace = TRUE),
                 mid = sample(11:20, 24, replace = TRUE),
                 max = sample(21:30, 24, replace = TRUE))
library(ggplot2)
library(purrr)

# list of the rows you want printing, this could be automated

plot_range <- list(p1_6 = 1:6, p7_12 = 7:12, p13_18  = 13:18, p19_24 = 19:24)

# plotting function which also sets a title and plot name

gg_plot <- function(df, plot_rows){

title <- paste("Automatic plot rows: ", min(plot_rows), "to", max(plot_rows))
plot_nm <- paste("plots", min(plot_rows), max(plot_rows), sep = "_")
    
p <- ggplot(df[plot_rows, ])+
  geom_segment(aes(x = min , xend = max, y = g, yend = g))+
  geom_point(aes(x = mid, y = g))+
  ggtitle(title)

print(ggsave(plot_nm, p, device = "pdf"))

}

# purrr function which acts as a loop to print each graph and allows a different data frame to be used.

walk(plot_range, ~gg_plot(df = df, plot_rows = .x))
#> Saving 7 x 5 in image
#> NULL
#> Saving 7 x 5 in image
#> NULL
#> Saving 7 x 5 in image
#> NULL
#> Saving 7 x 5 in image
#> NULL

reprex package (v0.3.0) 于 2020 年 7 月 11 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2021-11-05
    • 2016-06-22
    • 1970-01-01
    • 2021-05-26
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多