-1
我想了解以下段,尤其是一种萃取grobs结构的功能,sapply(tmp$grobs, function(x) x$name
问题上的理解使用sapply
require(gridExtra)
require(ggplot2)
my_hist <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
tmp <- ggplot_gtable(ggplot_build(my_hist))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
我觉得这个功能是用来提取的tmp$grobs
的名字,这是如下
然后我试图。第一个问题,如何理解结构,如[[1]]
;此外,为什么(tmp$grobs[1])$name
只是不起作用。
使用'[[':'$ TMP grobs [[1]] $ name'应该工作。 – agstudy 2014-09-21 20:56:31
'[1]'不起作用,因为'tmp'是一个列表,请尝试'typeof(tmp)'。请阅读[this r-faq](http://stackoverflow.com/questions/1169456/in-r-what-is-the-difference-between-the-and-notations-for-accessing-the)。然后,阅读[this r-faq](http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega) – 2014-09-21 21:02:36