在R版本3.2.2下安装arrangeGrob存在问题

3

我已经更新了我的R版本,包括所有的packages。函数arrangeGrob(Package gridExtra)已经发生了变化。

在我的旧版本的R version 3.1.3中,我使用如下方式来制作角落标签:

加载r packages

library(ggplot2)
library(grid)
library(gridExtra)

示例数据

a <- 1:20
b <- sample(a, 20)
c <- sample(b, 20)
d <- sample(c, 20)

创建数据框架。
mydata   <- data.frame(a, b, c, d)

创建示例图表

myplot1  <- ggplot(mydata, aes(x=a, y=b)) + geom_point()
myplot2  <- ggplot(mydata, aes(x=b, y=c)) + geom_point()
myplot3  <- ggplot(mydata, aes(x=c, y=d)) + geom_point()
myplot4  <- ggplot(mydata, aes(x=d, y=a)) + geom_point()

设置角标签

 myplot1 <- arrangeGrob(myplot1, main = textGrob("A", x = unit(0, "npc")
     , y   = unit(1, "npc"), just=c("left","top"),
     gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))

 myplot2 <- arrangeGrob(myplot2, main = textGrob("B", x = unit(0, "npc")
     , y = unit(1, "npc"), just=c("left","top"),
     gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))

 myplot3 <- arrangeGrob(myplot3, main = textGrob("C", x = unit(0, "npc")
    , y  = unit(1, "npc"), just=c("left","top"),
    gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))

 myplot4 <- arrangeGrob(myplot4, main = textGrob("D", x = unit(0, "npc")
    , y = unit(1, "npc"), just=c("left","top"),
    gp=gpar(col="black",    fontsize=18, fontfamily="Times Roman")))

 grid.arrange(myplot1, myplot2, myplot3, myplot4) 

我得到了以下的图表,看起来很好:

enter image description here

但在新的R版本3.2.2下,图片看起来像这样:

enter image description here

arrangeGrob为每个textGrob打开一个新的图像,我得到了八张图片而不是四张。如何修复它,使图表看起来像旧版本的R和gridExtra?


6
gridExtra已经进行了重写,这可能导致不完全向后兼容的问题。请查看新的Wiki https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html 。尝试将"main"更改为"top"。 - user20650
非常感谢,它有效了!!! - Kev
很棒的东西,请随意将其写成答案。 - user20650
当我尝试将结果保存为对象 g <-arrangeGrob(myplot1, myplot2, myplot3, myplot4) 时,我得到了一个以文本形式打印的 TableGrob,需要使用 plot(g) 来查看它。 - Dave X
Doh -- https://dev59.com/3lwZ5IYBdhLWcg3wbv7r 中提到你需要使用 grid.draw(g) 来代替。 - Dave X
1个回答

1

来自Kev的评论:

gridExtra已经进行了重写,这可能导致不完全向后兼容的问题。请查看新的维基页面cran.r-project.org/web/packages/gridExtra/vignettes/… 。尝试将main更改为top - user20650


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接