I am trying to understand the following segment, especially the one, sapply(tmp$grobs, function(x) x$name
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]]
I think this function is used to extract the name of tmp$grobs
, which is shown as follows
Then I tried. First question, how to understand the structure, like [[1]]
; further, why (tmp$grobs[1])$name
just does not work.