我试图找出带有heightDetails()的ggplot图例的高度,但是我得到了错误
UseMethod中的错误(" absolute.units"): 没有适用于'绝对。单位'的方法应用于类" unit"
的对象
示例:对于提取,我使用了函数g_legend建议here:
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
假设我们有一个简单的条形图
testplot <- ggplot(mtcars, aes(x=as.factor(gear), fill=as.factor(gear))) + geom_bar()
,我用
testlegend <- g_legend(testplot)
获得传奇。 is.grob(testlegend)表明它确实是一个grob,而grid.draw(testlegend)工作得很好。但是heightDetails(testlegend)给了我上面提到的异常。
我的猜测是testlegend $ heights包含一个单位的总和,而heightDetails不能处理它。这是什么原因?如果没有,为什么它不起作用?还有什么方法可以检索图例的高度?
答案 0 :(得分:1)
我相信你正在使用旧版本的grid / R / gtable;更新应修复错误。话虽如此,没有人知道为什么在这里使用absolute.units,根据我的经验,它给出了一个不准确的答案。您可能会发现,对gtable高度求和比调用heightDetails方法更可靠。