3

I'm trying to arrange (via grid.arrange in gridExtra) a ternary plot from ggtern-package and a regular ggplot2 plot side-by-side. However, the aesthetics and label positions of the ternary plot are removed.

I'm aware that this seems to be a bug. Any pointers to circumvent this issue and produce the output I'm looking for is much appreciated.

A reproducible example:

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

The ternary plot should look like:

print(tern.plot)

tern.plot

Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • 2
    Try [multiplot](http://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2-in-r/8391998#8391998), I've tested it on your plots and it worked perfectly – David Arenburg Jul 24 '14 at 11:12
  • the problem is arguably with ggtern, it defines a new `ggplot_build`, but not `ggplotGrob`, so that function is taken from ggplot2 with its own `ggplot_build` – baptiste Jul 24 '14 at 11:19
  • 1
    multiplot doesn't have the same problem because it restricts the input to ggplots, not general grobs, and draws them using a print method redefined in ggtern. I would argue that it would be cleaner to use different function names in ggtern, rather than override them with new, sometimes incompatible functionality. – baptiste Jul 24 '14 at 11:21
  • @DavidArenburg Thanks! Can you write it as an answer and I'll choose it for posterity. It seems that `ggtern.multi` also does the trick. – Anders Ellern Bilgrau Jul 24 '14 at 21:42
  • 1
    @AEBilgrau, I don't think that copy pasting someone elses answer is considered an answer :) This is why I've put it comments in the first place... Thus I think you should select your own answer as it could be useful for others who haven't heard of `ggtern.multi` – David Arenburg Jul 24 '14 at 21:54

1 Answers1

3

I got the wanted result using ggtern.multi which I apparently had completely missed.

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

Wanted result

As suggested in the comments by David Arenburg, the multiplot function also works perfectly.

library(devtools)
source_gist("https://gist.github.com/AEBilgrau/2a78a9ebda2226b6b988")
multiplot(tern.plot, stacked.plot, cols = 2)
Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37