wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
701 stars 84 forks source link

Plot aspect is different in Rstudio environment than when I save the plot #158

Open Dekike opened 4 years ago

Dekike commented 4 years ago

After running the code below, I see a different plot in my Rstudio environment compared to the plot I see when I save that plot (P5) on my computer. Is that a bug or is it a mistake of myself?

library(ggplot2)
library(cowplot)
library(ggpubr)
theme_set(theme_cowplot())

df1 <- data.frame(x = 1:12, y = (1:12)^2)
df1$grp = c('A', 'B', 'C')

df2 <- data.frame(x = 1:12, y = (1:12)^2)
df2$grp = c('D', 'E')

df3 <- data.frame(x = 1:12, y = (1:12)^2)
df3$grp = c('F', 'G', 'H','I')

df4 <- data.frame(x = 1:12, y = (1:12)^2)
df4$grp = c('J', 'K')

p1 <- ggplot(df1, aes(x, y, color=grp)) + geom_point()
leg1 <- get_legend(p1)
leg1 <- as_ggplot(leg1)
p1 = p1 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())

p2 <- ggplot(df2, aes(x, y, color=grp)) + geom_point()
leg2 <- get_legend(p2)
leg2 <- as_ggplot(leg2)
p2 = p2 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())

p3 <- ggplot(df3, aes(x, y, color=grp)) + geom_point()
leg3 <- get_legend(p3)
leg3 <- as_ggplot(leg3)
p3 = p3 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())

p4 <- ggplot(df4, aes(x, y, color=grp)) + geom_point()
leg4 <- get_legend(p4)
leg4 <- as_ggplot(leg4)
p4 = p4 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())

P <-plot_grid(
  p1, p1, p1, leg1, 
  p2, p2, p2, leg2,
  p3, p3, p3, leg3,
  p4, p4, p4, leg4,
  ncol = 4, rel_widths=c(2,2,2,0.5),align = "hv"
)  + theme(plot.margin = margin(55, 10, 30, 30))

P1 <- add_sub(P, "x axis text", x=0.5, y=-0.5, vpadding = grid::unit(0, "lines"))
P2 <- add_sub(P1, "y axis text", x=-0.02, y=-23, vpadding = grid::unit(-5, "lines"), angle = 90)
P3 <- add_sub(P2, "row 1", x=0.17, y=45, vpadding = grid::unit(0, "lines"),vjust = 1)
P4 <- add_sub(P3, "row 2", x=0.47, y=46, vpadding = grid::unit(0, "lines"),vjust = 1)
P5 <- add_sub(P4, "row 3", x=0.81, y=-25.4, vpadding = grid::unit(-2, "lines"),vjust = 1)
ggdraw(P5)

What I see in my computer Captura de pantalla de 2020-05-25 12-09-31

What I see when I save the plot Rplot