0

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

Z.Lin
  • 25,090
  • 5
  • 44
  • 85
Na5H
  • 13
  • 3
  • 1
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Apr 20 '19 at 17:58
  • 1
    Try to define `pipe_age` (and `pipe_age0`) as variable of your data frame, i.e. `net_ini$pipe_age_0 = 2019 + net.ini$time.construction` and `net_ini$pipe_age = 2019 - net.ini$pipe_age_0`! – Simon Apr 20 '19 at 20:55
  • Hi everyone, and thanks for giving me good advice. I decided to upload my work on google drive, and here is the [link](https://drive.google.com/file/d/1xeuD_6DNW1TkjVGCOdz2RjEqtFVw30-x/view?usp=sharing) so you can download it, try to run it on your computer and figure out what could be a problem, if you are still interested. I'm new to R, programming in general, second month to be honest, and still learning a lot. Cheers ;D – Na5H Apr 22 '19 at 13:50
  • @Na5H, it's bad practice to ask people to download files to reproduce. There could be malicious code or at the least it's much more inconvenient than making a minimal example. One of the benefits of creating a minimal example is you often find out what was going wrong in the first place. – JoeTheShmoe Sep 17 '20 at 16:42

1 Answers1

1

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

Utkarsh
  • 11
  • 1