0
votes

I'm trying to use cowplot to combine some ggplot2 plots. It should be straightforward, but something in my R or Rstudio surly is broken. What I don't know. I can get it to work with grid.arrange, but the output in my rmarkdown file does not come out as nicely. I broke down my code to the minimum amount to recreate the error, and out of rmarkdown

library(ggplot2)
library(Hmisc)
library(cowplot)


x <- c(1, 8, 9)
y <- c(1, 5, 9)
supply1 <- data.frame(bezier(x, y, evaluation = 500))

g1 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
  geom_path(data = supply1, aes(x = x, y = y), size = 1, colour = "BLUE")

g2 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
  geom_path(data = supply1, aes(x = x+1.5, y = y+1.5), size = 1, colour = "RED")


plot_grid(g1, g2,
          ncol = 2, 
          nrow = 1)

I get this error:

Error in FUN("text"[[1L]], ...) :

Theme element 'text' has NULL property: margin, debug

I have to detach cowplot, but can get something close with gridExtra using this code:

library(ggplot2)
library(Hmisc)
library(gridExtra)


x <- c(1, 8, 9)
y <- c(1, 5, 9)
supply1 <- data.frame(bezier(x, y, evaluation = 500))

g1 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
  geom_path(data = supply1, aes(x = x, y = y), size = 1, colour = "BLUE")

g2 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
  geom_path(data = supply1, aes(x = x+1.5, y = y+1.5), size = 1, colour = "RED")

grid.arrange(g1,g2,
             ncol = 2,
             nrow = 1)

This code outputs: grid.arrange plot

Turns out I get the "Error in FUN message" if I try to make any ggplot with both the ggplot2 and cowplot libraries loaded. R 3.1.3, RStudio 0.99.903, cowplot 0.4.0, ggplot2 2.1.0

I have reinstalled everything at least twice, and get the same error situation on a different computer. I can get it to work in a limited fashion. If I wait to call the cowplot library after all other code is run except the plot_grid() chunk, then it will knit and give me the cowplot output. I can not recreate this in a R script only in Rmarkdown, but then I have to have it be the final chunk of the markdown, any ggplot attempts after it will cause the knit to fail.

Short term I used grid.arrange() and just lived with the results, long term I would like to have cowplot as an option.

Any ideas or suggestions?

1
I've had the same problem, look at jrnold's answer about half way down: github.com/jrnold/ggthemes/issues/57Nate
I can't reproduce this error in either interactive mode or in an rmarkdown document. I'm running R 3.3.1, RStudio 1.0.44, ggplot2 2.1.0, and cowplot 0.6.3.eipi10
Works fine with: R 3.3.2, Windows7, cowplot_0.7.0, Hmisc_4.0-0, ggplot2_2.2.0zx8754
I upgraded R to 3.3.1 and R studio to 1.0.44 and it went away.Nick

1 Answers

1
votes

Apparently it's a bug that has been fixed since R 3.3.1 so upgrade to this version or newer and it should go away.