Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

altering height of rows produced by grid.arrange when nrow=1

Tags:

r

ggplot2

Apologies if this question has already been answered but I cant seem to find what I need.

I have produced two plots in ggplot2 which I am combining into the same grid using grid.arrange as follows:

grid.arrange(p1,p2,main="Title", ncol=2)

which gives me the plots side by side like so:

Side by side plots

(Sorry I don't understand how to get this to show my image within the post, if you anyone could help me with that as an aside that would be great! I don't want to annoy people by using links.)

How can I alter this code so that the graphs are still side by side but they are not elongated the entire length of the object? I would like them to be square.

I know that I can add an argument "heights" but not sure if this is what I need and haven't seen anything on here applying it in this situation.

Thanks!

like image 314
Nicki Avatar asked Apr 22 '13 13:04

Nicki


2 Answers

You can also specify relative heights and widths using the heights and widths arguments to grid.arrange like so:

grid.arrange(p1 , p2 , widths = unit(0.5, "npc") , heights=unit(0.5, "npc") , main="Title", ncol=2)

enter image description here

like image 53
Simon O'Hanlon Avatar answered Nov 02 '22 12:11

Simon O'Hanlon


As you are using ggplot2 to make plots then one way would be to use coord_fixed() to get quadratic plots and then arrange them. You can achieve this with coord_fixed() where ratio= is calculated by dividing range of y values by range of x values.

ratio.plot1<-abs(max(iris$Petal.Width)-min(iris$Petal.Width))/abs(max(iris$Petal.Length)-min(iris$Petal.Length))

ratio.plot2<-abs(max(iris$Sepal.Width)-min(iris$Sepal.Width))/abs(max(iris$Sepal.Length)-min(iris$Sepal.Length))

p1<-ggplot(iris,aes(Petal.Width,Petal.Length))+geom_point()+
  coord_fixed(ratio=ratio.plot1)
p2<-ggplot(iris,aes(Sepal.Width,Sepal.Length))+geom_point()+
  coord_fixed(ratio=ratio.plot2)
grid.arrange(p1,p2,main="Title",ncol=2)

enter image description here

like image 29
Didzis Elferts Avatar answered Nov 02 '22 13:11

Didzis Elferts