0

I have these two codes:

v <- melt(tvalB)
data1 <- as.data.frame(cbind(tval, v$value))
sizez <- 14
ggplot(data = data1, aes(x =T-value)) +
  geom_density(aes(tval, fill = "Actual"), alpha = 0.5, adjust=8) +
  geom_density(aes(V2, fill = "Simulation"), alpha = 0.5, adjust=8) +
  scale_colour_manual("", breaks = c("Actual", "Simulation"),
                  values = c("red", "blue")) +
  scale_y_continuous("density") + 
  scale_x_continuous("t(alpha)", limits = c(-6.5,6.5)) +
  labs(title ="") +
  theme(plot.title = element_text(hjust = 0.5)) +
  guides(fill=guide_legend(title=NULL)) + 
  theme(legend.title=element_text(size=sizez+1),legend.text=element_text(size=sizez),
    axis.text=element_text(size=sizez-1), axis.title=element_text(size=sizez), 
    legend.key = element_rect(size = 0.05),
    legend.key.height = unit(1.25, "cm"),
    legend.key.width = unit(1.25, "cm"))

and

data1 %>% 
  gather(key, value) %>% 
  ggplot(aes(value, color=key)) + 
  stat_ecdf(size=0.8)+ xlim(-4.5,4.5) + 
  labs(x = "t(alpha)", y = "probability", color=NULL) + 
  scale_colour_discrete(labels = c("Actual","Simulation")) + 
  theme(legend.title=element_text(size=sizez+1), legend.text=element_text(size=sizez),
    axis.text=element_text(size=sizez-1), axis.title=element_text(size=sizez), 
    legend.key = element_rect(size = 4),
    legend.key.height = unit(1.3, "cm"),
    legend.key.width = unit(1.3, "cm"))data1 %>% 
  gather(key, value) %>% 
  ggplot(aes(value, color=key)) + 
  stat_ecdf(size=0.8)+ xlim(-4.5,4.5) + 
  labs(x = "t(alpha)", y = "probability", color=NULL) + 
  scale_colour_discrete(labels = c("Actual","Simulation")) + 
  theme(legend.title=element_text(size=sizez+1), legend.text=element_text(size=sizez),
    axis.text=element_text(size=sizez-1), axis.title=element_text(size=sizez), 
    legend.key = element_rect(size = 4),
    legend.key.height = unit(1.3, "cm"),
    legend.key.width = unit(1.3, "cm"))

It produces these two plots:

CDF plot

PDF plot

As it appears from the pictures, the gray area in the two plots doesn`t have equal heights. Is there some way to predefine the plot margins, such that if the plots are placed next to each other, in a report, they are identical in height, etc. Thank you in advance!!

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
Mads
  • 123
  • 2
  • 7
  • https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots/39298897#39298897 – tjebo May 27 '18 at 19:54
  • 1
    Also, you're not entirely new to SO, and you're risking down voting when asking questions: - without sample data and - with so long and very very likely a lot of unnecessary code – tjebo May 27 '18 at 19:55
  • 1
    [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung May 27 '18 at 19:56
  • Possible duplicate of [How to set limits for axes in ggplot2 R plots?](https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots) – tjebo May 27 '18 at 19:57
  • This seems to be a classic use-case for `cowplot`: see https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html – Weihuang Wong May 28 '18 at 00:55

1 Answers1

1

Here's a short example. We create two plots, where I've added a new line to the x-axis label in the second plot to make the plot areas different heights:

library(ggplot2)
p1 <- ggplot(mtcars, aes(x = mpg)) +
  stat_ecdf()
p2 <- ggplot(mtcars, aes(x = mpg)) +
  stat_density() +
  labs(x = "mpg\n")

We use the grid.arrange function from the gridExtra package to see how the plots look side by side:

gridExtra::grid.arrange(p1, p2, nrow = 1)

enter image description here

The plot_grid function from the cowplot package allows us to align the axes:

cowplot::plot_grid(p1, p2, align = "h")

enter image description here

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48