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

前提是你 提交于 2019-12-12 08:38:13

问题


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!


回答1:


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)




回答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)



来源:https://stackoverflow.com/questions/16148076/altering-height-of-rows-produced-by-grid-arrange-when-nrow-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!