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:
(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!
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With