使用ggplot绘制三元图时出现意外输出

3
我想通过gridExtra中的grid.arrange来排列一个三元图和一个常规的ggplot2图形。然而,三元图的美学和标签位置被移除了。
知道这似乎是一个错误。非常感谢任何可以解决这个问题并产生我所期望的输出的指针。
一个可重现的例子:
library(ggplot2)
library(ggtern)
library(reshape2)
library(gridExtra)

# Some faux data
dat <- replicate(3, runif(5))
dat <- as.data.frame(dat/rowSums(dat))
colnames(dat) <- LETTERS[seq_len(ncol(dat)) + 23]
dat$var <- factor(LETTERS[seq_len(nrow(dat))])

# Make a ternary plot
tern.plot <- 
  ggplot(data = dat, aes(y=Y, x=X, z=Z, color = var, fill = var)) +
  coord_tern() +
  geom_point(size = 3)

# Make a stacked barplot
dat.melt <- melt(dat, id.vars = "var", variable.name = "dim")  
stacked.plot <-
  ggplot(data = dat.melt, aes(x = var, y = value, fill = dim)) +
  geom_bar(stat = "identity")

# Arrange the two plots:
grid.arrange(tern.plot, stacked.plot, ncol = 2)
#Warning messages:
#1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
#2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
# ...
#9: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

grid.arrange

三元图应该长这样:
print(tern.plot)

tern.plot


2
尝试使用multiplot,我已经在你的图表上测试过了,它完美地工作了。 - David Arenburg
问题可以说是与ggtern有关,它定义了一个新的ggplot_build,但没有定义ggplotGrob,因此该函数会从ggplot2中使用其自己的ggplot_build - baptiste
1
multiplot没有同样的问题,因为它将输入限制为ggplots而不是一般的grobs,并使用在ggtern中重新定义的print方法进行绘制。我认为,在ggtern中使用不同的函数名称会更加清晰,而不是用新的、有时不兼容的功能来覆盖它们。 - baptiste
@DavidArenburg 谢谢!你能写成答案吗?我会选择它作为永久记录。似乎ggtern.multi也可以解决问题。 - Anders Ellern Bilgrau
1
@AEBilgrau,我认为复制粘贴别人的答案并不被视为答案 :) 这就是为什么我首先将其放在评论中的原因... 因此,我认为您应该选择自己的答案,因为它可能对那些没有听说过ggtern.multi的人有用 - David Arenburg
1个回答

3

我使用ggtern.multi得到了想要的结果,这显然是我完全错过的。

ggtern.multi(tern.plot, stacked.plot, cols = 2)

期望结果

正如David Arenburg在评论中建议的那样,multiplot函数也可以完美地工作。

library(devtools)
source_gist("https://gist.github.com/AEBilgrau/2a78a9ebda2226b6b988")
multiplot(tern.plot, stacked.plot, cols = 2)

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