Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

r - removing all the space between two ggplots combined with grid.arrange

I want to stick two plots without any space between theme (so they share one axis).

Given:

p1 <- qplot(1,1,xlab="")

p1 <- p1 +
  theme(legend.position="none",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,0,1), "cm"),
        panel.margin=unit(c(1,1,0,1), "cm"))
p2 <- qplot(1,2)

grid.arrange(p1,p2)

Which produces:

enter image description here

I want to eliminate the white space between the two plots.

I have the impression tweaking heights, as has been done for widths in : left align two graph edges (ggplot) is the solution, but can't figure it out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should provide plot.margin for both plots and set negative value for the bottom margin for p1 and upper margin for p2. This will ensure that both plot joins.

p1 <-  qplot(1,1,xlab="")+
  theme(legend.position="none",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,-0.5,1), "cm"))
p2 <- qplot(1,2)+
  theme(legend.position="none",
        plot.margin=unit(c(-0.5,1,1,1), "cm"))


grid.arrange(p1,p2)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...