绘图列表包含多个副本的最后一个绘图。

huangapple go评论60阅读模式
英文:

Plot list contains multiple copies of last plot

问题

我正在尝试在R中对具有可变列数的数据集进行分析。分析的一部分是线性回归,为了确保结果合理,我想要分别可视化每列的数据。

我已经为分析设置了一个循环,并使用ggsave将每个图保存为pdf文件 - 到目前为止,一切正常,有72个不同的pdf文件,每个文件都有独立的图。

但如果我想要创建一个图形列表,并使用ggarrange来查看它们,我得到的只是一个图,其中包含了最后一个图的72个副本。

library(ggplot2)
library(dplyr)
library(ggpubr)
rm(list = ls())
#选择数据(csv格式,第一列是周期号)
filename <- file.choose()
Data<-read.csv(filename)
#从所有数据中减去第一行(需要dplyr)
Results<-data.frame(matrix(ncol=2,nrow=0, dimnames=list(NULL, c("Well", "Slope"))))
Data %>% mutate_if(is.numeric, funs(.-first(.)))
#基于周期号6到25的子集
Data<-Data[c(6:25),]
yy <-ncol(Data)
plotList=list()
for (i in seq(from = 2, to = yy, by = 1)){
well<-Data[,i]
lm_fit <- lm(well ~ Cycle, data=Data)
cf <- coef(lm_fit)
Slope <- cf[2]
WellData<-merge(i,Slope)
Results<-rbind(Results, WellData)
pltName <- paste( 'Plot', i, sep = '' )
P1 <-eval(substitute(ggplot(Data, aes(Cycle, well)) + 
geom_point() + geom_smooth(method="lm")),list(i=i))
plotList[[i]]<-P1
filename<-(paste0(pltName,".pdf"))
ggsave(filename = filename)
}
colnames(Results) <- c("Well", "Slope")
Results[1]<-Results[1]-1
print(Results)
ggarrange(plotlist = plotList)

示例数据:

Cycle    A1    A2    A3    A4
1    5451.07    5986.14    5922.59    7092.01
2    5953.04    6527.92    6505.93    7989.85
3    6227.3    6752.74    6783.56    8237.67
4    6477.02    6952.62    6959.91    8302.8
5    6660.14    7133.86    7149.98    8286.72
6    6804.14    7288.28    7301.48    8271.56
7    6919.78    7413.27    7424.77    8264.33
8    7002.38    7511.45    7519.84    8245.55
9    7075.04    7597.66    7599.81    8229.94
10    7127.82    7645.51    7649.2    8212.87
11    7171.94    7695.83    7701.47    8211.97
12    7193.82    7727.9    7730.65    8199.28
13    7229.76    7756.09    7762.58    8185.75
14    7252.7    7783.35    7780.36    8193.05
15    7259.97    7793    7789.77    8179.77
16    7278.49    7803.85    7799.26    8177.83
17    7288.07    7814.82    7799.02    8169.31
18    7294.38    7812.15    7810.12    8169.02
19    7295.29    7821.13    7816.68    8177.07
20    7296.58    7826.55    7816.37    8165.72
21    7304.65    7829.22    7823.11    8169.64
22    7305.38    7821.46    7819.89    8167.32
23    7306.26    7828.97    7820.36    8162.02
24    7306.38    7830.81    7823.79    8162.48
25    7300.14    7824.72    7827.94    8165.42
26    7296.08    7821.81    7816.32    8156.62
27    7301.82    7826.25    7816.6    8160.69
28    7295.56    7829.91    7822.93    8153.53
29    7295.81    7827.21    7809.87    8156.48
30    7299.35    7833.84    7821.41    8162.64
31    7300.85    7834.91    7819.91    8156.94
英文:

I'm trying to run an analysis in R on a dataset with a variable number of columns. One part of the analysis is a linear regression, and to ensure the results make sense I would like to visualise the data from each column separately.

I've set up a loop for the analysis, and use ggsave to save each plot as a pdf - so far, all good, 72 distinct pdfs with individual plots.
But if I want to create a list of plots, and use ggarrange to view them, all I get is a plot with 72 (assuming 72 columns of data!) copies of the last plot.

Code:

library(ggplot2)
library(dplyr)
library(ggpubr)
rm(list = ls())
#Select data (csv format, first column cycle numbers)
filename &lt;- file.choose()
Data&lt;-read.csv(filename)
#subtract first row from all data (requires dplyr)
Results&lt;-data.frame(matrix(ncol=2,nrow=0, dimnames=list(NULL, c(&quot;Well&quot;, &quot;Slope&quot;))))
Data %&gt;% mutate_if(is.numeric, funs(.-first(.)))
#subset based on cycles 6 to 25
Data&lt;-Data[c(6:25),]
yy &lt;-ncol(Data)
plotList=list()
for (i in seq(from = 2, to = yy, by = 1)){
well&lt;-Data[,i]
lm_fit &lt;- lm(well ~ Cycle, data=Data)
cf &lt;- coef(lm_fit)
Slope &lt;- cf[2]
WellData&lt;-merge(i,Slope)
Results&lt;-rbind(Results, WellData)
pltName &lt;- paste( &#39;Plot&#39;, i, sep = &#39;&#39; )
P1 &lt;-eval(substitute(ggplot(Data, aes(Cycle, well)) + 
geom_point() + geom_smooth(method=&quot;lm&quot;)),list(i=i))
plotList[[i]]&lt;-P1
filename&lt;-(paste0(pltName,&quot;.pdf&quot;))
ggsave(filename = filename)
}
colnames(Results) &lt;- c(&quot;Well&quot;, &quot;Slope&quot;)
Results[1]&lt;-Results[1]-1
print(Results)
ggarrange(plotlist = plotList)

Example data:

Cycle	A1	A2	A3	A4
1	5451.07	5986.14	5922.59	7092.01
2	5953.04	6527.92	6505.93	7989.85
3	6227.3	6752.74	6783.56	8237.67
4	6477.02	6952.62	6959.91	8302.8
5	6660.14	7133.86	7149.98	8286.72
6	6804.14	7288.28	7301.48	8271.56
7	6919.78	7413.27	7424.77	8264.33
8	7002.38	7511.45	7519.84	8245.55
9	7075.04	7597.66	7599.81	8229.94
10	7127.82	7645.51	7649.2	8212.87
11	7171.94	7695.83	7701.47	8211.97
12	7193.82	7727.9	7730.65	8199.28
13	7229.76	7756.09	7762.58	8185.75
14	7252.7	7783.35	7780.36	8193.05
15	7259.97	7793	7789.77	8179.77
16	7278.49	7803.85	7799.26	8177.83
17	7288.07	7814.82	7799.02	8169.31
18	7294.38	7812.15	7810.12	8169.02
19	7295.29	7821.13	7816.68	8177.07
20	7296.58	7826.55	7816.37	8165.72
21	7304.65	7829.22	7823.11	8169.64
22	7305.38	7821.46	7819.89	8167.32
23	7306.26	7828.97	7820.36	8162.02
24	7306.38	7830.81	7823.79	8162.48
25	7300.14	7824.72	7827.94	8165.42
26	7296.08	7821.81	7816.32	8156.62
27	7301.82	7826.25	7816.6	8160.69
28	7295.56	7829.91	7822.93	8153.53
29	7295.81	7827.21	7809.87	8156.48
30	7299.35	7833.84	7821.41	8162.64
31	7300.85	7834.91	7819.91	8156.94

答案1

得分: 0

你遇到了惰性评估的问题,这意味着你的计数变量 i 只在打印图表时进行评估,例如请参考 Multiple ggplots in one page using for Loop in R

作为替代方法,你可以使用 lapply

library(ggplot2)
library(ggpubr)

Data <- subset(Data, Cycle %in% 6:25)

plotList <- lapply(names(Data)[-1], function(x) {
  ggplot(Data, aes(Cycle, .data[[x]])) +
    geom_point() +
    geom_smooth(method = "lm") +
    labs(title = x)
})

ggarrange(plotlist = plotList)

或者作为第二种不同的方法,你可以将数据重塑为长格式并使用 facetting。由于你想创建一堆图,我建议你也看看 ggforce::facet_wrap_paginate,它允许将分面绘图拆分到多个页面上。

Data %>%
  tidyr::pivot_longer(-1, names_to = "well") %>%
  ggplot(aes(Cycle, value)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(~well, scales = "free_y")

数据

Data <- read.table(text = "Cycle   A1  A2  A3  A4
1   5451.07 5986.14 5922.59 7092.01
2   5953.04 6527.92 6505.93 7989.85
3   6227.3  6752.74 6783.56 8237.67
4   6477.02 6952.62 6959.91 8302.8
5   6660.14 7133.86 7149.98 8286.72
6   6804.14 7288.28 7301.48 8271.56
7   6919.78 7413.27 7424.77 8264.33
8   7002.38 7511.45 7519.84 8245.55
9   7075.04 7597.66 7599.81 8229.94
10  7127.82 7645.51 7649.2  8212.87
11  7171.94 7695.83 7701.47 8211.97
12  7193.82 7727.9  7730.65 8199.28
13  7229.76 7756.09 7762.58 8185.75
14  7252.7  7783.35 7780.36 8193.05
15  7259.97 7793    7789.77 8179.77
16  7278.49 7803.85 7799.26 8177.83
17  7288.07 7814.82 7799.02 8169.31
18  7294.38 7812.15 7810.12 8169.02
19  7295.29 7821.13 7816.68 8177.07
20  7296.58 7826.55 7816.37 8165.72
21  7304.65 7829.22 7823.11 8169.64
22  7305.38 7821.46 7819.89 8167.32
23  7306.26 7828.97 7820.36 8162.02
24  7306.38 7830.81 7823.79 8162.48
25  7300.14 7824.72 7827.94 8165.42
26  7296.08 7821.81 7816.32 8156.62
27  7301.82 7826.25 7816.6  8160.69
28  7295.56 7829.91 7822.93 8153.53
29  7295.81 7827.21 7809.87 8156.48
30  7299.35 7833.84 7821.41 8162.64
31  7300.85 7834.91 7819.91 8156.94", header = TRUE)
英文:

You are running into the issue of lazy evaluation, which means that your counter variable i gets only evaluated when your print your plots, see e.g. Multiple ggplots in one page using for Loop in R.

As an alternative you could use lapply:

library(ggplot2)
library(ggpubr)

Data &lt;- subset(Data, Cycle %in% 6:25)

plotList &lt;- lapply(names(Data)[-1], function(x) {
  ggplot(Data, aes(Cycle, .data[[x]])) +
    geom_point() +
    geom_smooth(method = &quot;lm&quot;) +
    labs(title = x)
})

ggarrange(plotlist = plotList)

绘图列表包含多个副本的最后一个绘图。<!-- -->

Or as a second and different approach you could reshape your data to long and use facetting. And as you want to create a bunch of plots I would also suggest to have a look at ggforce::facet_wrap_paginate which allows to split a facetted plot over multiple pages.

Data |&gt; 
  tidyr::pivot_longer(-1, names_to = &quot;well&quot;) |&gt; 
  ggplot(aes(Cycle, value)) +
  geom_point() +
  geom_smooth(method = &quot;lm&quot;) +
  facet_wrap(~well, scales = &quot;free_y&quot;)

绘图列表包含多个副本的最后一个绘图。<!-- -->

DATA

Data &lt;- read.table(text = &quot;Cycle   A1  A2  A3  A4
1   5451.07 5986.14 5922.59 7092.01
2   5953.04 6527.92 6505.93 7989.85
3   6227.3  6752.74 6783.56 8237.67
4   6477.02 6952.62 6959.91 8302.8
5   6660.14 7133.86 7149.98 8286.72
6   6804.14 7288.28 7301.48 8271.56
7   6919.78 7413.27 7424.77 8264.33
8   7002.38 7511.45 7519.84 8245.55
9   7075.04 7597.66 7599.81 8229.94
10  7127.82 7645.51 7649.2  8212.87
11  7171.94 7695.83 7701.47 8211.97
12  7193.82 7727.9  7730.65 8199.28
13  7229.76 7756.09 7762.58 8185.75
14  7252.7  7783.35 7780.36 8193.05
15  7259.97 7793    7789.77 8179.77
16  7278.49 7803.85 7799.26 8177.83
17  7288.07 7814.82 7799.02 8169.31
18  7294.38 7812.15 7810.12 8169.02
19  7295.29 7821.13 7816.68 8177.07
20  7296.58 7826.55 7816.37 8165.72
21  7304.65 7829.22 7823.11 8169.64
22  7305.38 7821.46 7819.89 8167.32
23  7306.26 7828.97 7820.36 8162.02
24  7306.38 7830.81 7823.79 8162.48
25  7300.14 7824.72 7827.94 8165.42
26  7296.08 7821.81 7816.32 8156.62
27  7301.82 7826.25 7816.6  8160.69
28  7295.56 7829.91 7822.93 8153.53
29  7295.81 7827.21 7809.87 8156.48
30  7299.35 7833.84 7821.41 8162.64
31  7300.85 7834.91 7819.91 8156.94&quot;, header = TRUE)

huangapple
  • 本文由 发表于 2023年6月16日 08:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定