使用ggplot排列三元图时出现意外输出

时间:2014-07-24 10:41:01

标签: r plot ggplot2 gridextra

我试图安排(通过grid.arrange gridExtra)来自ggtern的三元图 - 包和常规ggplot2情节并排。然而,三元图的美学和标签位置被删除。

我' m aware that this seems to be a bug.我们非常感谢能够避开这个问题并产生我想要的输出的任何指示。

可重现的例子:

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

1 个答案:

答案 0 :(得分:3)

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

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

Wanted result

正如David Arenburg的评论所述,multiplot功能也很完美。

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