Na5H
Na5H

Reputation: 13

grid.arrange shows empty plots

I have created some plots using ggplot2 in R. Now, I want to combine two plots using grid.arrange, however after I use this function I get only empty plots.

I also tried cowplot library but I am getting same problem.

## Pipe age distribution in the initial pipe network conditions
pipe_age_0 = 2019 + net.ini$time.construction
pipe_age = 2019 - pipe_age_0

p6 <- ggplot(net.ini, aes(x = pipe_age))
p6 + geom_histogram(binwidth = 1,
                    col="black",
                    size=0.1,
                    fill = "lightgrey") +
     scale_x_continuous(breaks = seq(from = 0, to = 140, by = 10)) +
     labs(y="Count", x="Age [Years]") +
     theme_bw(base_size = 12, base_family = "")

## Density stacked with histogram count
p7 <- ggplot(net.ini, aes(x = pipe_age))
p7 + geom_histogram(aes(y = ..density..),
                    binwidth = 1,
                    col="black",
                    size=0.1,
                    fill = "lightgrey") +
  geom_density(alpha = 0.1, fill="#FF6666") +
  scale_x_continuous(breaks = seq(from = 0, to = 140, by = 10)) +
  labs(y="Density", x="Age [Years]") +
  theme_bw(base_size = 12, base_family = "")

grid.arrange(p6, p7)

I expect to have two graphs one above another, or one beside another using grid.arrange. However for some reason I keep getting empty plot. This is how plots p6 and p7 look like, and third one with grid.arrange, the empty plot:

image

Upvotes: 0

Views: 1142

Answers (2)

Onion
Onion

Reputation: 1

I had the exact same issue. And I know you asked this 4 years ago, but I'll share what helped me just in case other people still experience something similar.

The only thing that helped me fix the empty plot problem was adjusting the code I wrote to create the plots. Instead of making the ggplot line its own line, combine with the lines below that include the specifications for the plot. For example, your p6 plot should look like this:

p6 <- ggplot(net.ini, aes(x = pipe_age)) + 
geom_histogram(binwidth = 1,
                    col="black",
                    size=0.1,
                    fill = "lightgrey") +
     scale_x_continuous(breaks = seq(from = 0, to = 140, by = 10)) +
     labs(y="Count", x="Age [Years]") +
     theme_bw(base_size = 12, base_family = "")

As you can see the only difference is that I deleted the 2nd "p6" and added a plus symbol to connect the lines together. After I did this with my own assignment, it worked.

Upvotes: 0

mrNobody
mrNobody

Reputation: 56

Don't know why, but it works only after we have assigned it back to the variable. Didn't work when I used the simple

p <- ggplot(df)
p + geom_col(aes(x,y))

So try something like this:

p7 <- ggplot(net.ini, aes(x = pipe_age))
p7 <- p7 + geom_histogram(aes(y = ..density..),
                    binwidth = 1,
                    col="black",
                    size=0.1,
                    fill = "lightgrey") +
  geom_density(alpha = 0.1, fill="#FF6666") +
  scale_x_continuous(breaks = seq(from = 0, to = 140, by = 10)) +
  labs(y="Density", x="Age [Years]") +
  theme_bw(base_size = 12, base_family = "")

Works for both cowplot and grid.arrange

Upvotes: 1

Related Questions