在ggplot中对齐绘图区域 [英] Align plot areas in ggplot

查看:126
本文介绍了在ggplot中对齐绘图区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用grid.arrange在ggplot生成的同一页上显示多个图。这些图使用相同的x数据但具有不同的y变量。由于y数据具有不同的比例,因此这些图表出现了不同的尺寸。



我已经尝试在ggplot2中使用各种主题选项来更改绘图大小并移动y轴标签,但没有任何工作对齐的情节。我希望将这些图放置在一个2 x 2的方格中,以便每个图都具有相同的尺寸,并且x轴对齐。

以下是一些测试数据:

  A <-c(1,5,6,7,9)
B < - c(10,56 ,64,86,98)
C < - c(2001,3333,5678,4345,5345)
D < - c(13446,20336,24333,34345,42345)
(L,A,B,C,D)



以及我正在使用的代码:

 <$ c $ (M,aes(L,A,xmin = 10,ymin = 0))+ geom_point()+ stat_smooth(method ='1m')
x2< -ggplot aes(L,B,xmin = 10,ymin = 0))+ geom_point()+ stat_smooth(method ='lm')
x3 < - ggplot (M,aes(L,D,xmin = 10,ymin = 0))+ geom_point()+ geom_point()+ stat_smooth(method ='lm')
x4 < - ggplot stat_smooth(method ='lm')
grid.arrange(x1,x2,x3,x4,nrow = 2)

如果你运行这段代码,你会发现最下面的两个地块有一个sma由于y轴单位的长度更大,所以ller绘图区域。

如何使实际绘图窗口相同?
<

  library(

)解决方案

重塑2)
dat < - 熔体(M,L)#如果有疑问,熔化!
$ b $ ggplot(dat,aes(L,value))+
geom_point()+
stat_smooth(method =lm)+
facet_wrap(〜variable, ncol = 2,scales =free)



注意:外行可能会错过刻度之间的刻度不同。

I am trying to use grid.arrange to display multiple graphs on the same page generated by ggplot. The plots use the same x data but with different y variables. The plots come out with differing dimensions due to the y-data having different scales.

I have tried using various theme options within ggplot2 to change the plot size and move the y axis label but none have worked to align the plots. I want the plots arranged in a 2 x 2 square so that each plot is the same size and the x-axes align.

Here is some test data:

A <- c(1,5,6,7,9)
B <- c(10,56,64,86,98)
C <- c(2001,3333,5678,4345,5345)
D <- c(13446,20336,24333,34345,42345)
L <- c(20,34,45,55,67)
M <- data.frame(L, A, B, C, D)

And the code that I am using to plot:

x1 <- ggplot(M, aes(L, A,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x2 <- ggplot(M, aes(L, B,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x3 <- ggplot(M, aes(L, C,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x4 <- ggplot(M, aes(L, D,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
grid.arrange(x1,x2,x3,x4,nrow=2)

If you run this code, you will see that the bottom two plots have a smaller plot area due to the greater length of the y-axes units.

How do I make the actual plot windows the same?

解决方案

I would use faceting for this problem:

library(reshape2)
dat <- melt(M,"L") # When in doubt, melt!

ggplot(dat, aes(L,value)) + 
geom_point() + 
stat_smooth(method="lm") + 
facet_wrap(~variable,ncol=2,scales="free")

Note: The layman may miss that the scales are different between facets.

这篇关于在ggplot中对齐绘图区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆