R 使用正在由Lappy循环的ggplot命名绘图
我正在使用R 使用正在由Lappy循环的ggplot命名绘图,r,ggplot2,R,Ggplot2,我正在使用lappy 假设我有这个函数 plot <- function(x){ ggplot(x,aes(x=timepoints,means)) + geom_point() + geom_line() + scale_x_continuous(name='some name') + scale_y_continuous(name='another name') + geom_errorbar(aes(ymi
lappy
假设我有这个函数
plot <- function(x){
ggplot(x,aes(x=timepoints,means)) +
geom_point() +
geom_line() +
scale_x_continuous(name='some name') +
scale_y_continuous(name='another name') +
geom_errorbar(aes(ymin=means-stderrs,ymax=means+stderrs),width=2) +
opts(title = names(x))
}
我的问题是:
my ggplot函数的names(x)
部分将每个绘图命名为“means”,而不是该数据帧的实际名称(例如name1、name2)
函数位于list[[i]]
中,我需要实际数据帧的名称,即names(list[i])
问题:
可以说,有没有一种方法可以引用列表中的外层?或者有没有一种更简单的方法来获得这些图的列表,以及它们各自的名称?虽然不漂亮,但它很有效
plot2 <- function(x, title){
ggplot(x,aes(x=timepoints,means)) +
geom_point() +
geom_line() +
scale_x_continuous(name='some name') +
scale_y_continuous(name='another name') +
geom_errorbar(aes(ymin=means-stderrs,ymax=means+stderrs),width=2) +
opts(title = title)
}
lapply(names(your_list), function(x) plot2(your_list[[x]], x))
plot2您很少需要在ggplot
中使用此结构。你能用一个完整的例子来展开你的问题吗?包括你要划分的初始列表?看起来你要画一个点,它有一个未定义的x
坐标,时间点。您始终可以将另一项添加到包含该名称的列表中。但最简短的回答是否定的,lappy
只查看列表元素内部的数据,而不知道外部列表或其名称。或者使用lappy(seq_沿着(你的列表),…)
并按照你描述的方式进行索引。这是可行的。它需要对您的代码思想进行一些更改
plot2 <- function(x, title){
ggplot(x,aes(x=timepoints,means)) +
geom_point() +
geom_line() +
scale_x_continuous(name='some name') +
scale_y_continuous(name='another name') +
geom_errorbar(aes(ymin=means-stderrs,ymax=means+stderrs),width=2) +
opts(title = title)
}
lapply(names(your_list), function(x) plot2(your_list[[x]], x))