英文:
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 <- file.choose()
Data<-read.csv(filename)
#subtract first row from all data (requires dplyr)
Results<-data.frame(matrix(ncol=2,nrow=0, dimnames=list(NULL, c("Well", "Slope"))))
Data %>% mutate_if(is.numeric, funs(.-first(.)))
#subset based on cycles 6 to 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)
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 <- 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)
<!-- -->
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 |>
tidyr::pivot_longer(-1, names_to = "well") |>
ggplot(aes(Cycle, value)) +
geom_point() +
geom_smooth(method = "lm") +
facet_wrap(~well, scales = "free_y")
<!-- -->
DATA
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论