Warm tip: This article is reproduced from stackoverflow.com, please click
ggplot2 heatmap plot r

Change size (width) of plot in ggplot

发布于 2020-11-13 08:42:04

I am a newbie attempting to change the width of a ggplot, so that I am can arrange different plots(heatmap and dotplot) in the same figure. However, after hours of trying to reduce the width of the dotplot, I am about to give up.

Code for heatmap (maybe not relevant):

heatmap_GO_NES_1<-ggplot(data=long_frame_GO_NES_1) +
  geom_tile(mapping = aes(
    x = factor(timepoint,levels = c("6h","12h","24h")),
    y =bio_process,fill = NES)) +
  ylab(label="Biological process") + 
  theme(axis.title.x=element_blank()) +
  scale_fill_gradient(low="red",high="green")+
  facet_grid( group ~. , scales="free",space="free")+
  theme(axis.text.x = element_text(angle = 90))+
  theme(strip.text.y = element_text(size = 8))

heatmap_GO_NES_1

enter image description here

Code for dotplot:

dot_GO_NES_1<- ggplot(data=long_frame_GO_NES_2)+
  geom_count(mapping=aes(x=timepoint, y =bio_process, size=setsize))+
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
    axis.ticks.x=element_blank(),axis.title.y=element_blank(),
    axis.text.y=element_blank(),axis.ticks.y=element_blank())

dot_GO_NES_1

enter image description here

Code for figure:

plot_grid(heatmap_GO_NES_1,dot_GO_NES_1)

enter image description here

Obviously, the dotplot is stealing all the figure space, so that my heatmap does not show up in the figure.

Questioner
Rikki Franklin Frederiksen
Viewed
1
chemdork123 2020-07-08 00:24

TL;DR - you need to use the rel_widths= argument of plot_grid(). Let me illustrate with an example using mtcars:

# Plots to display
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()

p2 <- ggplot(mtcars, aes(x='X', y=disp)) + geom_point(aes(size=cyl))

Here are the plots, where you see p2 is like your plot... should not be too wide or it looks ridiculous. This is the default behavior of plot_grid(), which makes both plots the same width/relative size:

plot_grid(p1,p2)

enter image description here

Adjust the relative width of the plots using rel_widths=:

plot_grid(p1,p2, rel_widths=c(1,0.3))

enter image description here