我有一個資料框架,我想對每一列做一個boxplot,除了前兩列,但是它們將被用于其他目的。
我的資料框架如下
df < -結構(list(SAMPLE_NO =結構(c(1L。 1L, 1L,, 1L, 2L,
2L,/span> 2L。 2L, 2L。 3L, 3L。 3L, 3L。 3L, 4L。 4L, 4L。 4L, 4L。 5L, 5L,
5L,/span> 5L。 5L, 6L。 6L, 6L。 6L, 6L。 7L, 7L。 7L, 7L。 7L, 8L。 8L, 8L, 8L,
8L, 8L, 9L。 9L, 9L, 9L。 9L, 10L, 10L。 10L, 10L。 10L), 。 標簽 = c("137380",.
"137796",/span> "137926"。 "138180",/span> "138217"。 "138233",/span> "138569"。 "138978",
"139128", "139137")。 class = "factor")。 Repeat_No = c(1L, 2L,
3L, 4L。 5L, 1L。 2L, 3L。 4L, 5L。 1L, 2L。 3L, 4L。 5L, 1L。 2L, 3L,
4L,/span> 5L。 1L, 2L。 3L, 4L。 5L, 1L。 2L, 3L。 4L, 5L。 1L, 2L。 3L, 4L,
5L, 1L。 2L, 3L。 4L, 5L。 1L, 2L。 3L, 4L。 5L, 1L。 2L, 3L。 4L, 5L[/span
), Fe = c(59。 78, 59.59, 59. 76, 59.49, 59.72, 59. 74, 59.52, 59.85,
59.73, 59.61, 59. 71, 59.76, 59. 73, 59.56, 59. 82, 59.71, 59.6,
59.86, 59.78, 59. 84, 59.49, 59. 75, 59.71, 59. 73, 59.51, 59.75,
59.77, 59.83, 59. 55, 59.6, 59. 68, 59.57, 59. 73, 59.76, 59.58,
59.48, 59.79, 59. 73, 59.64, 59. 85, 59.75, 59. 68, 59.63, 59.73,
59.78, 59.59, 59. 69, 59.79, 59. 7), SiO2 = c(5. 1, 5.14,
5.11, 5.14, 5. 14, 5.12, 5.13, 5. 11, 5.09, 5. 12, 5.15, 5.13,
5.11, 5.14, 5. 11, 5.1, 5.12, 5. 1, 5.11, 5.12, 5. 15, 5.16, 5.11,
5.12, 5.17, 5. 11, 5.15, 5.12, 5. 15, 5.12, 5.17, 5. 12, 5.13, 5.12, 5.12, 5.1, 5. 1, 5.14, 5. 11, 5.1, 5.14, 5. 15, 5.07, 5.1,
5.1, 5。 11, 5.12, 5. 11)。 Al2O3 = c(2. 08, 2.09, 2. 09, 2.1, 2.06,
2.08, 2.07, 2。 09, 2.06, 2. 09, 2.11, 2.07, 2. 07, 2.09, 2.06,
2.07, 2.09, 2。 1, 2.08, 2. 09, 2.08, 2.07, 2. 07, 2.07, 2.1,
2.07, 2.07, 2. 1, 2.07, 2.11, 2. 09, 2.07, 2.11, 2. 06, 2.09, 2.09,
2.08, 2.05, 2。 08, 2.07, 2.08, 2. 1, 2.08, 2. 1, 2.05, 2.07,
2. 08),TiO2 = c(0。 14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0.14, 0.14 ,
0.14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0.14, 0.14 ,
0.14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0.14, 0.14 ,
0.14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0.14, 0.14 ,
0.14, 0。 14, 0.14, 0. 14, 0.14, 0. 14, 0.14, 0. 14)),行。 names = c(NA,)
-50L)。 class = "data. frame")
我還有一個資料幀串列,其中有一些我想用來在每個boxplots上畫線的值,即平均值和sd.
。我的資料幀串列
df2 < -list(Fe =結構(c(59. 6938, 0. 103625897007515), . Dim = 1:2, 。 Dimnames = list() 。
NULL, c("hm"。 "hsd")))。 SiO2 =結構(c(5. 121, 0.0214998813475606。
), . Dim = 1:2, 。 Dimnames = list(NULL。 c("hm"/span>。 "hsd")))。 Al2O3 =結構(c(2。 0812,.
0.015069905136275), 。 Dim = 1:2, 。 Dimnames = list(NULL。 c("hm",
"hsd")))。 TiO2 =結構(c(0。 14, 0), 。 Dim = 1:2, 。 Dimnames = list() 。
NULL, c("hm"。 "hsd"))))
和我的代碼,使箱形圖
plotorder < -排序(unique(df2$SAMPLE_NO))
imap(df2, ~{)
ggplot(df, outlier.shape = NA,
mapping = aes(x = SAMPLE_NO, y = . data[[/span]. y]]。 color = SAMPLE_NO))
stat_boxplot(geom = 'errorbar')
coord_cartesian(ylim = as. numeric(c(min(. y),max(. y))) 。
geom_boxplot(outlier.shape = NA)
geom_hline(linetype = 'dashed'。 顏色 = 'blue', 大小 = 0. 75,
mapping = aes(yintercept = as.numeric(. x[[1]]。 標點符號">)) #mean
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = as.numeric(. x[[1]]) #mean。
- (as.numeric(. x[[2]/span>])) * 3)) # subtract SD
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = as.numeric(. x[[2]>。
(as.numeric(. x[[2]])) * 3)) # Add SD
ggtitle(paste0(. y, " Homogeniety Box Plot") .
主題(plot.title = element_text(hjust = 0。 5))
xlab(label = "Sample No")
ylab(paste0(. y, ' values %'))。
}) ->/span> list_plot_box
串列_繪圖_框
[! [在此輸入圖片描述][ 1]][/span>1]
這產生了三個列的boxplot,但不是第一個列,而且每個圖表的線條都位于錯誤的lcoation中。
uj5u.com熱心網友回復:
c(min(.y), max(.y))是不正確的,因為.y指的是'df2'的名稱,即 "Fe", ...
imap(df2, ~{)
ggplot(df, outlier.shape = NA,
mapping = aes(x = SAMPLE_NO, y = . data[[/span]. y]]。 color = SAMPLE_NO))
stat_boxplot(geom = 'errorbar')
coord_cartesian(ylim = as. numeric(c(min(df[. y]])。 max(df[. y]]))))
geom_boxplot(outlier.shape = NA)
geom_hline(linetype = 'dashed', 顏色 = 'blue', 大小 = 0. 75,
mapping = aes(yintercept = as.numeric(. x[[1]]。 標點符號">)) #mean
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = as.numeric(. x[[1]]) #mean。
- (as.numeric(. x[[2]/span>])) * 3)) # subtract SD
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = as.numeric(. x[[1]]) #mean value。
(as.numeric(. x[[2]])) * 3)) # Add SD
ggtitle(paste0(. y, " Homogeniety Box Plot") .
主題(plot.title = element_text(hjust = 0。 5))
xlab(label = "Sample No")
ylab(paste0(. y, ' values %'))。
}) ->/span> list_plot_box
-檢查第一個list元素的輸出
> list_plot_box[[1]]
關于沒有顯示的行,只是ylimit與第一個行相比,差別太小,所以沒有顯示。 如果我們去掉* 3,它就會顯示
imap(df2, ~{)
ggplot(df, outlier.shape = NA,
mapping = aes(x = SAMPLE_NO, y = . data[[/span]. y]]。 color = SAMPLE_NO))
stat_boxplot(geom = 'errorbar')
coord_cartesian(ylim = as. numeric(c(min(df[. y]])。 max(df[. y]]))))
geom_boxplot(outlier.shape = NA)
geom_hline(linetype = 'dashed', 顏色 = 'blue', 大小 = 0. 75,
mapping = aes(yintercept = as.numeric(. x[[1]]。 標點符號">)) #mean
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = as.numeric(. x[[1]]) #mean。
- (as.numeric(. x[[2]])) ) # subtract SD
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = as.numeric(. x[[1]]) #mean value。
(as.numeric(. x[[2]])) ) # Add SD
ggtitle(paste0(. y, " Homogeniety Box Plot") 。
主題(plot.title = element_text(hjust = 0。 5))
xlab(label = "Sample No")
ylab(paste0(. y, ' values %'))。
}) ->/span> list_plot_box
-檢查
list_plot_box[[1]]
我們可以在單頁中獲得所有的圖畫
library(ggpubr)
ggarrange(plotlist = list_plot_box。 ncol = 2, nrow = 2)
uj5u.com熱心網友回復:
最后,我用我需要的繪圖引數創建了一個資料框架,然后將它們映射到GGPLot來制作每個圖表
df2 <- $Fe
hm hsd hmin hmax
[1,] 59. 6938 0.1036259 59.38292 60.00468
$SiO2
hm hsd hmin hmax
[1,] 5. 121 0.02149988 5.0565 5.1855
$Al2O3
hm hsd hmin hmax
[1,] 2。 0812 0.01506991 2.03599 2.12641[/span
$TiO2
hm hsd hmin hmax
[1,] 0. 14 0 0.14 0.14
然后把我的主要代碼改成
imap(df2, ~{)
ggplot(df, outlier.shape = NA,
mapping = aes(x = SAMPLE_NO, y = . data[[/span]. y]]。 color = SAMPLE_NO))
stat_boxplot(geom = 'errorbar')
coord_cartesian(ylim = as. numeric(c(. x[3],。 x[4]/span>)))
geom_boxplot(outlier.shape = NA)
geom_hline(linetype = 'dashed'。 顏色 = 'blue', 大小 = 0. 75,
mapping = aes(yintercept = as.numeric(. x[[1]]。 標點符號">)) #mean
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = . x[4]/span> )) # subtract SD
geom_hline(linetype = 'dashed'/span>, 顏色 = 'firebrick', 尺寸 = 0。 75,
mapping = aes(yintercept = . x[3] )) # Add SD
ggtitle(paste0(. y, " Homogeniety Box Plot") .
主題(plot.title = element_text(hjust = 0。 5))
xlab(label = "Sample No")
ylab(paste0(. y, ' values %'))。
}) ->/span> list_plot_box
list_plot_box[[1]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/334054.html
標籤:



