我有一個資料框串列,(datalist)并且在 ggplot 中創建了包裝圖,geom_line但是我正在努力為每個圖分配其標題(BA0、OG0、ON0 等)。
List of 27
$ BA0 :'data.frame': 14587 obs. of 2 variables:
..$ V1 : int [1:14587] 1 2 1 1 2 1 2 1 1 1 ...
..$ V2 : int [1:14587] 43 45 46 48 49 53 55 56 57 58 ...
$ OG0 :'data.frame': 7925 obs. of 2 variables:
..$ V1 : int [1:7925] 1 1 1 1 1 2 5 7 4 10 ...
..$ V2 : int [1:7925] 43 53 84 88 90 91 92 93 94 95 ...
$ ON0 :'data.frame': 8347 obs. of 2 variables:
..$ V1 : int [1:8347] 1 2 10 6 6 3 11 6 6 6 ...
..$ V2 : int [1:8347] 96 97 98 99 100 101 102 103 104 105 ...
這是代碼
names(datalist) <- c("BA0", "OG0", "ON0", ...)
graph<-lapply(datalist,function(x)
p<-ggplot(x,aes(x= V2,y= V1))
geom_line(colour = "blue")
labs(x = "read length", y="occurences")
scale_x_continuous(n.breaks =10)
scale_y_continuous(n.breaks = 8)
)
wrap_plots(graph)
我試圖添加到p選項:
ggtitle(datalist$file) ## here I added an extra column to each df corresponding to the names: BA0, OG0, ON0 etc..
ggtitle(names(datalist))
labs(title = names(datalist))
但我只能為每個地塊擁有相同的標題,BA0,它是串列的第一個元素

如何添加正確的標題?或者也許有更好的方法來創建這一系列情節?
謝謝
uj5u.com熱心網友回復:
您可以enframe將資料框串列放入一個表中,并title使用該函式創建一個新列dplyr::mutate。然后,您可以使用purrr::pmap將列元素映射到創建繪圖的函式公式。最后,繪圖列可以輸入patchwork::wrap_plots:
library(tidyverse)
library(patchwork)
data <-
list(
iris1 = iris,
iris2 = iris
)
str(data)
#> List of 2
#> $ iris1:'data.frame': 150 obs. of 5 variables:
#> ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#> $ iris2:'data.frame': 150 obs. of 5 variables:
#> ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
data %>%
enframe() %>%
mutate(
title = paste0("Fancy title ", name),
plt = list(title, value) %>% pmap(~ {
.y %>%
ggplot(aes(Sepal.Length, Sepal.Width))
geom_line()
labs(title = .x)
})
) %>%
pull(plt) %>%
wrap_plots()

由reprex 包于 2022-05-25 創建 (v2.0.0 )
或者,也可以為繪圖創建一個顯式函式:
my_plot <- function(title, data) {
data %>%
ggplot(aes(Sepal.Length, Sepal.Width))
geom_line()
labs(title = title)
}
data %>%
enframe() %>%
mutate(
title = paste0("Fancy title ", name),
plt = list(title, value) %>% pmap(my_plot)
)
#> # A tibble: 2 × 4
#> name value title plt
#> <chr> <list> <chr> <list>
#> 1 iris1 <df [150 × 5]> Fancy title iris1 <gg>
#> 2 iris2 <df [150 × 5]> Fancy title iris2 <gg>
uj5u.com熱心網友回復:
使用facet_wrap可能是合適的。你可以試試
library(data.table)
library(ggplot2)
#from list to dataframe, adding name of list item as column
dt <- rbindlist(datalist, idcol = TRUE)
#plot using facet_wrap
ggplot(dt,aes(x= V2,y= V1))
geom_line(colour = "blue")
labs(x = "read length", y="occurences")
scale_x_continuous(n.breaks =10)
scale_y_continuous(n.breaks = 8)
facet_wrap(~.id)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/481211.html
上一篇:如何根據屬性減去兩個物件串列?
