问题描述
我有一个数据帧列表:
str(subsets.d)
List of 22
$ 1 :'data.frame': 358 obs. of 118 variables:
..$ Ac_2017_1 : num [1:358] 0 0 0 0 0 0 0 0 0 0 ...
..$ Ac_9808_1 : num [1:358] 0 0 0 0 0 ...
..$ dates : Ord.factor w/ 6 levels "April"<"May"<..: 1 1 1 1 1 1 1
$ 19 :'data.frame': 358 obs. of 2 variables:
..$ Ac_8598_19: num [1:358] 0.000257 0.000288 0.000171 0 0.000562 ...
..$ dates : Ord.factor w/ 6 levels "April"<"May"<..: 1 1 1 1 1 1 1
我将其转换为长格式:
library(reshape2)
subsets.m <- lapply(subsets.d, function(x) melt(x))
然后我想按日期生成箱形图:
and then i want to produce boxplots by dates:
library(ggplot2)
lapply(subsets.m, function(x)
ggplot(x, aes(dates, value)) + geom_boxplot() + facet_wrap(~variable, scale="free_y"))
是否可以将ggsave()
或pdf()
放在lapply的内部或上方?如果是这样,我如何根据数据框命名保存的图?
Is it possible to put a ggsave()
or pdf()
inside or otside the lapply? if so, how can i name the saved plots according to the dataframe?
奖金问题,由于列表内的数据框具有不同的尺寸,因此图的尺寸可能会有所不同.我可以适应吗?
Bonus question, since the dataframes inside the list have different dimensions, the plots can differ in size. Can i adjust for that?
以下是一些小示例数据:
Here is some small example data:
dput(list1)
list(structure(list(Ac_7595_JG37 = c(0.000128383, 0.000576914,
0.000341631, 0.000729133, 0.000187486, 0.00086127, 0.000594978,
0.000631912, 0.000502274, 0.0004846, 0, 0.000148386, 0.000298153,
0.000828969, 0.000436815, 7.28336e-05, 0.000304842, 0.000301909,
0.000233694, 0.000208491), Ac_7474_JG37 = c(0, 0, 0.000512446,
0, 0.000562457, 0, 0.000198326, 0.000473934, 0, 0.000138457,
0.000132554, 0, 0.000198769, 0.000207242, 0.000145605, 0.000364168,
0, 0.000301909, 0.000116847, 0), dates = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("April", "May", "June", "August", "October",
"November"), class = c("ordered", "factor"))), .Names = c("Ac_7595_JG37",
"Ac_7474_JG37", "dates"), row.names = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "61", "62", "63", "64", "65", "66",
"67", "68", "69", "70"), class = "data.frame"), structure(list(
Ac_8732_20 = c(0.000513534, 0.000384609, 0.000341631, 0.000729133,
0.000937429, 0.000322976, 0.000297489, 0.000394945, 0.000669698,
0.000346143, 0.000265108, 0.000296773, 0.000596306, 0.000310863,
0, 0.000509835, 0, 0.000301909, 0.000233694, 0), dates = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("April", "May", "June", "August",
"October", "November"), class = c("ordered", "factor"))), .Names = c("Ac_8732_20",
"dates"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "61", "62", "63", "64", "65", "66", "67", "68", "69",
"70"), class = "data.frame"))
推荐答案
您的图存储在列表中,因此您可以在输出上使用lapply来保存所有图.此处已回答以下问题:按名称保存图块列表()
Your plots are stored in a list, hence you can use lapply on the output to save all of them. This has been answered here: Saving a list of plots by their names()
# create data for this example (data above too involved)
df <- data.frame(value = rnorm(100), dates = rep(1:50, 2), type = rep(c("a", "b")))
list1 <- split(df, df$type)
plots <- lapply(list1, function(x) ggplot(x, aes(dates, value)) + geom_boxplot())
lapply(names(plots),
function(x) ggsave(filename=paste(x,".jpeg",sep=""), plot=plots[[x]]))
这篇关于在lapply中保存地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!