snippet of data.frame snippet of list of data.frames
我的列表如下:
> dput(lapply(list1, head))
list(structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(6L,
4L), .Dimnames = list(NULL, c("FPAR", "gpp", "LAI", "npp"))),
structure(c(63L, 83L, 66L, 84L, 92L, 85L, 9976L, 3318L, 9456L,
9435L, 9002L, 9395L, 21L, 32L, 18L, 34L, 50L, 36L, 6742L,
5228L, 5405L, 5136L, 5387L, 5339L), .Dim = c(6L, 4L), .Dimnames = list(
NULL, c("FPAR", "gpp", "LAI", "npp"))), structure(c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(6L, 4L), .Dimnames = list(
NULL, c("FPAR", "gpp", "LAI", "npp"))), structure(c(0L,
95L, 0L, 82L, 0L, 82L, 10306L, 10205L, 10306L, 10627L, 10306L,
10627L, 0L, 64L, 0L, 31L, 0L, 31L, 6396L, 6340L, 6396L, 6396L,
6396L, 6396L), .Dim = c(6L, 4L), .Dimnames = list(NULL, c("FPAR",
"gpp", "LAI", "npp"))), structure(c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
7331L, NA, NA), .Dim = c(6L, 4L), .Dimnames = list(NULL,
c("FPAR", "gpp", "LAI", "npp"))), structure(c(96L, 96L,
100L, 88L, 93L, 100L, 12734L, 12215L, 11383L, 11446L, 12672L,
11531L, 61L, 61L, 70L, 40L, 58L, 70L, 7807L, 7357L, 7695L,
6400L, 6009L, 7735L), .Dim = c(6L, 4L), .Dimnames = list(
NULL, c("FPAR", "gpp", "LAI", "npp"))), structure(c(95L,
95L, 96L, 96L, 96L, 95L, 10829L, 10829L, 9652L, 10321L, 9652L,
10829L, 61L, 61L, 62L, 65L, 62L, 61L, 5144L, 5144L, 5188L,
5422L, 5188L, 5144L), .Dim = c(6L, 4L), .Dimnames = list(
NULL, c("FPAR", "gpp", "LAI", "npp"))), structure(c(NA,
NA, 87L, 63L, 87L, 87L, NA, NA, 7891L, 7891L, 0L, 7891L,
NA, NA, 46L, 19L, 46L, 46L, NA, NA, NA, NA, NA, NA), .Dim = c(6L,
4L), .Dimnames = list(NULL, c("FPAR", "gpp", "LAI", "npp"
))), structure(c(100L, 100L, 100L, 78L, 100L, 100L, 4011L,
4011L, 4112L, 4306L, 3664L, 4112L, 70L, 70L, 70L, 21L, 70L,
70L, 2253L, 2425L, 2479L, NA, 2253L, 2253L), .Dim = c(6L,
4L), .Dimnames = list(NULL, c("FPAR", "gpp", "LAI", "npp"
))), structure(c(83L, 87L, 71L, NA, 0L, 82L, 5627L, 6626L,
5862L, NA, 6963L, 5541L, 29L, 37L, 16L, NA, 0L, 28L, 3877L,
3695L, 3635L, 1642L, 3692L, 3644L), .Dim = c(6L, 4L), .Dimnames = list(
NULL, c("FPAR", "gpp", "LAI", "npp"))), structure(c(88L,
89L, 61L, NA, 0L, 88L, 6132L, 5636L, 6166L, NA, 6510L, 6502L,
49L, 51L, 18L, NA, 0L, 50L, 3413L, 3788L, 3519L, NA, 3463L,
3755L), .Dim = c(6L, 4L), .Dimnames = list(NULL, c("FPAR",
"gpp", "LAI", "npp"))))
因此,我试图为列表中的11个数据帧有效地绘制变量1(FPAR)和变量2-4(gpp,LAI,npp)之间的散点图。我已经附上了清单的摘要。
这是我到目前为止提出的,但是没有用。
library(ggplot2)
scatterplot <- function(yvar){
ggplot(list1, aes_(x=~age, y=as.name(yvar))) +
geom_point() +
facet_wrap(~dataframe)
}
lapply(list1, scatterplot)
这是我第一次使用列表和ggplot,所以有点卡住了。
答案 0 :(得分:2)
这是您可以执行此操作的方法。
library(tidyverse)
list1 %>%
map(., as_data_frame) %>%
bind_rows(., .id = "dataframe") %>%
gather(., key, value, gpp:npp) %>%
mutate(dataframe = factor(dataframe, levels = unique(dataframe))) %>%
ggplot(., aes(FPAR, value, col = key)) +
geom_point() +
facet_wrap(. ~ dataframe, ncol = 3)
警告: 删除了包含缺失值(geom_point)的71行。
您可能需要阅读:
答案 1 :(得分:1)
这不能回答您的问题,但可能会让您更进一步。您需要考虑函数的输入。如果list1
是data.frames
的列表,则scatterplot
函数的输入将是data.frame
。根据您的data.frame
结构,函数可能如下所示:
df <- data.frame(a = rnorm(n = 20),
b = rnorm(n = 20),
d = c(rep('first', 10),
rep('sec', 10)))
df_l <- list(df, df)
scatterplot <- function(df){
ggplot(df, aes(x = a, y = b)) +
geom_point() +
facet_wrap(~d)
}
lapply(df_l, scatterplot)
祝你好运:)