在R中的lapply调用中将列表名称添加为绘图标题 [英] Adding lists names as plot titles in lapply call in R

查看:160
本文介绍了在R中的lapply调用中将列表名称添加为绘图标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sample <- 
structure(list(GB05 = structure(c(22L, 34L, 26L, 2L), .Dim = 4L, .Dimnames = structure(list(
    c("98", "100", "102", "106")), .Names = ""), class = "table"), 
    GB18 = structure(c(8L, 14L, 70L), .Dim = 3L, .Dimnames = structure(list(
        c("173", "175", "177")), .Names = ""), class = "table"), 
    GB06 = structure(c(2L, 16L, 48L, 10L, 10L, 6L), .Dim = 6L, .Dimnames = structure(list(
        c("234", "238", "240", "242", "244", "246")), .Names = ""), class = "table"), 
    GB27 = structure(c(2L, 28L, 2L, 2L, 4L, 3L, 2L, 2L, 15L, 
    17L, 4L, 5L), .Dim = 12L, .Dimnames = structure(list(c("145", 
    "147", "149", "151", "156", "159", "165", "167", "169", "171", 
    "173", "175")), .Names = ""), class = "table"), GB24 = structure(c(2L, 
    4L, 41L, 10L, 6L, 2L, 14L, 2L, 3L), .Dim = 9L, .Dimnames = structure(list(
        c("240", "241", "242", "243", "244", "247", "249", "251", 
        "253")), .Names = ""), class = "table"), GB28 = structure(c(30L, 
    22L, 2L, 10L, 2L, 4L, 2L, 2L, 2L), .Dim = 9L, .Dimnames = structure(list(
        c("363", "364", "365", "367", "371", "377", "380", "384", 
        "390")), .Names = ""), class = "table"), GB15 = structure(c(12L, 
    16L, 43L, 2L, 3L, 4L), .Dim = 6L, .Dimnames = structure(list(
        c("142", "144", "146", "147", "148", "152")), .Names = ""), class = "table"), 
    GB02 = structure(c(6L, 78L, 4L), .Dim = 3L, .Dimnames = structure(list(
        c("194", "197", "200")), .Names = ""), class = "table"), 
    GB10 = structure(c(2L, 36L, 7L, 1L, 16L, 20L), .Dim = 6L, .Dimnames = structure(list(
        c("124", "126", "128", "130", "132", "134")), .Names = ""), class = "table"), 
    GB14 = structure(c(3L, 6L, 7L, 37L, 7L), .Dim = 5L, .Dimnames = structure(list(
        c("181", "184", "187", "193", "196")), .Names = ""), class = "table")), .Names = c("GB05", 
"GB18", "GB06", "GB27", "GB24", "GB28", "GB15", "GB02", "GB10", 
"GB14"))

如何在此lapply调用中使列表中的名称显示为地块标题(每个地块一个标题)?

How to get the names in this list to appear as plot titles in this lapply call (one title for each plot) ?

dev.new()
par(mfrow=c(2,5))
lapply(sample,function(x) plot(x,main=names[x]))

我不明白为什么main = names [x]不起作用.

I don't understand why main=names[x] won't work.

推荐答案

在列表项的名称上使用lapply:

Use lapply on the names of the list items instead:

lapply(names(afn), function(x) plot(afn[[x]], main=x))

要查看为什么,您没有得到自己的名字,请运行lapply(afn, function(x) names(x)),为您列出每个列表项或其他内容的名称.尝试与lapply(names(afn), function(x) x)相同,然后比较差异.然后,请记住,我们可以使用[[按名称提取单个列表项.

To see why you aren't getting your names, run lapply(afn, function(x) names(x)) gives you the names of each list item or something else. Try the same with lapply(names(afn), function(x) x) and compare the difference. Then, remember that we can extract a single list item by name by using [[.

这篇关于在R中的lapply调用中将列表名称添加为绘图标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆