R-marrangeGrob独特传奇人物

由于有gridExtra的marrangeGrob()函数,我可以在多个页面上排列45个ggplot对象。我想在每页上显示一个相同而独特的图例。

我知道如何提取图例(g_legend),如何在没有图例的情况下绘制ggplot。但是,由于marrangeGrob和独特的图例,我找不到一种多页的方法。

我用g_legend()提取了我的图例

g_legend<-function(a.gplot){
 g <- ggplotGrob(a.gplot + theme(legend.position = "right"))$grobs
 legend <- g[[which(sapply(g,function(x) x$name) == "guide-box")]]
 return(legend)}

为简化起见,可再生数据集来自钻石,我想在一个页面p1和p2以及另一个页面p2和p1上生成。

df <- count(diamonds,cut)

p1 = ggplot(df,aes(x=cut,y=n,label=format(n,big.mark=","),fill=cut)) +
geom_bar(stat="identity") +
geom_text(aes(y=0.5*n),colour="white") +
coord_flip() +
theme(legend.position="bottom")

p2 = ggplot(diamonds %>% sample_n(1000),aes(x=carat,y=price,colour=cut)) +
geom_point()

leg = g_legend(p1)

我首先尝试在一页上绘制4张图

combined<-arrangeGrob(do.call("arrangeGrob",c(
lapply(list(p1,p2,p1),function(x){ x + theme(legend.position="none")}),ncol=2)),leg,ncol = 2)
grid.newpage()
grid.draw(combined)

效果很好,但是当我尝试使用多页显示时

marrangeGrob(do.call("marrangeGrob",c(lapply(list(p1,ncol=2,nrow=2)),ncol = 2,nrow=1)

我获得了: $<-.data.frame*tmp*中的错误,“ wrapvp”,值= list(x = 0.5,y = 0.5,:替换有17行,数据有5

有人知道使用marrangeGrob并在每个多页上获得唯一图例的方法吗?

adayho 回答:R-marrangeGrob独特传奇人物

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3145261.html

大家都在问