R,ggplot - 共享相同y轴但具有不同x轴刻度的图形

时间:2013-02-07 03:33:00

标签: r ggplot2

上下文

我有一些数据集/变量,我想绘制它们,但我想以紧凑的方式做到这一点。要做到这一点,我希望它们共享相同的y轴但不同的x轴,并且由于不同的分布,我希望其中一个x轴是对数缩放而另一个是线性缩放。

实施例

假设我有一个长尾变量(我想在绘制时对x轴进行对数缩放):

library(PtProcess)
library(ggplot2)

set.seed(1)
lambda <- 1.5
a <- 1
pareto <- rpareto(1000,lambda=lambda,a=a)
x_pareto <- seq(from=min(pareto),to=max(pareto),length=1000)
y_pareto <- 1-ppareto(x_pareto,lambda,a)
df1 <- data.frame(x=x_pareto,cdf=y_pareto)

ggplot(df1,aes(x=x,y=cdf)) + geom_line() + scale_x_log10()

Long tail

一个正常的变量:

set.seed(1)
mean <- 3
norm <- rnorm(1000,mean=mean)
x_norm <- seq(from=min(norm),to=max(norm),length=1000)
y_norm <- pnorm(x_norm,mean=mean)
df2 <- data.frame(x=x_norm,cdf=y_norm)

ggplot(df2,aes(x=x,y=cdf)) + geom_line()

Normal

我想使用相同的y轴并排绘制它们。

尝试#1

我可以使用facet看起来很棒,但是我不知道如何使每个x轴具有不同的比例(scale_x_log10()使得它们都按比例缩放):

df1 <- cbind(df1,"pareto")
colnames(df1)[3] <- 'var'
df2 <- cbind(df2,"norm")
colnames(df2)[3] <- 'var'
df <- rbind(df1,df2)

ggplot(df,aes(x=x,y=cdf)) + geom_line() + 
       facet_wrap(~var,scales="free_x") + scale_x_log10()

Facets attempt

尝试#2

使用grid.arrange,但我不知道如何保持两个绘图区域具有相同的宽高比:

library(gridExtra)
p1 <- ggplot(df1,aes(x=x,y=cdf)) + geom_line() + scale_x_log10() +
      theme(plot.margin = unit(c(0,0,0,0), "lines"),
            plot.background = element_blank()) +
      ggtitle("pareto")
p2 <- ggplot(df2,aes(x=x,y=cdf)) + geom_line() + 
      theme(axis.text.y = element_blank(), 
            axis.ticks.y = element_blank(), 
            axis.title.y = element_blank(),
            plot.margin = unit(c(0,0,0,0), "lines"),
            plot.background = element_blank()) +
      ggtitle("norm")
grid.arrange(p1,p2,ncol=2)

Grid attempt

PS:地块数量可能会有所不同,所以我不是专门为2个地块寻找答案

3 个答案:

答案 0 :(得分:9)

扩展您的尝试#2,gtable可能会帮助您。如果两个图表中的边距相同,那么两个图中唯一的宽度(我认为)是y轴刻度标记和轴文本所采用的空间,这反过来会改变面板的宽度。使用here中的代码,轴文本占用的空间应该相同,因此两个面板区域的宽度应该相同,因此纵横比应该相同。但是,结果(右边没有边距)看起来不漂亮。所以我在p2的右边添加了一点边距,然后在p2的左边带走了相同的量。同样地,对于p1:我在左边添加了一点,但在右边带了相同的数量。

library(PtProcess)
library(ggplot2)
library(gtable)
library(grid)
library(gridExtra)

set.seed(1)
lambda <- 1.5
a <- 1
pareto <- rpareto(1000,lambda=lambda,a=a)
x_pareto <- seq(from=min(pareto),to=max(pareto),length=1000)
y_pareto <- 1-ppareto(x_pareto,lambda,a)
df1 <- data.frame(x=x_pareto,cdf=y_pareto)

set.seed(1)
mean <- 3
norm <- rnorm(1000,mean=mean)
x_norm <- seq(from=min(norm),to=max(norm),length=1000)
y_norm <- pnorm(x_norm,mean=mean)
df2 <- data.frame(x=x_norm,cdf=y_norm)

p1 <- ggplot(df1,aes(x=x,y=cdf)) + geom_line() + scale_x_log10() +
      theme(plot.margin = unit(c(0,-.5,0,.5), "lines"),
            plot.background = element_blank()) +
      ggtitle("pareto")
p2 <- ggplot(df2,aes(x=x,y=cdf)) + geom_line() + 
      theme(axis.text.y = element_blank(), 
            axis.ticks.y = element_blank(), 
            axis.title.y = element_blank(),
            plot.margin = unit(c(0,1,0,-1), "lines"),
            plot.background = element_blank()) +
      ggtitle("norm")

gt1 <- ggplotGrob(p1)
gt2 <- ggplotGrob(p2)

newWidth = unit.pmax(gt1$widths[2:3], gt2$widths[2:3])

gt1$widths[2:3] = as.list(newWidth)
gt2$widths[2:3] = as.list(newWidth)

grid.arrange(gt1, gt2, ncol=2)

enter image description here

修改 要向右添加第三个图,我们需要对绘图画布进行更多控制。一种解决方案是创建一个新的gtable,其中包含三个图的空间和一个右边距的额外空间。在这里,我让图中的边距处理图之间的间距。

p1 <- ggplot(df1,aes(x=x,y=cdf)) + geom_line() + scale_x_log10() +
      theme(plot.margin = unit(c(0,-2,0,0), "lines"),
            plot.background = element_blank()) +
      ggtitle("pareto")
p2 <- ggplot(df2,aes(x=x,y=cdf)) + geom_line() + 
      theme(axis.text.y = element_blank(), 
            axis.ticks.y = element_blank(), 
            axis.title.y = element_blank(),
            plot.margin = unit(c(0,-2,0,0), "lines"),
            plot.background = element_blank()) +
      ggtitle("norm")

gt1 <- ggplotGrob(p1)
gt2 <- ggplotGrob(p2)

newWidth = unit.pmax(gt1$widths[2:3], gt2$widths[2:3])

gt1$widths[2:3] = as.list(newWidth)
gt2$widths[2:3] = as.list(newWidth)

 # New gtable with space for the three plots plus a right-hand margin
gt = gtable(widths = unit(c(1, 1, 1, .3), "null"), height = unit(1, "null"))

# Instert gt1, gt2 and gt2 into the new gtable
gt <- gtable_add_grob(gt, gt1, 1, 1)
gt <- gtable_add_grob(gt, gt2, 1, 2)
gt <- gtable_add_grob(gt, gt2, 1, 3)

grid.newpage()
grid.draw(gt)

enter image description here

答案 1 :(得分:1)

当使用R进行绘图时,公认的答案正是使人们奔跑的原因!这是我的解决方案:

library('grid')
g1 <- ggplot(...)  # however you draw your 1st plot
g2 <- ggplot(...)  # however you draw your 2nd plot
grid.newpage()
grid.draw(cbind(ggplotGrob(g1), ggplotGrob(g2), size = "last"))

这可轻松处理y轴(辅助轴和主轴)的基准线,以便在多个图中对齐。

其他一些任务可以在创建单个图形时或通过使用gridgridExtra包提供的其他方法来处理,以除去一些轴文本,统一图例等。

答案 2 :(得分:1)

接受的答案对我来说有点太令人生畏了。所以我找到了两种方法来以较少的努力解决它。两者都基于您的 Attempt #2 grid.arrange() 方法。

1. 使 plot 1 也没有 y 轴
theme(axis.text.y = element_blank(), 
      axis.ticks.y = element_blank(), 
      axis.title.y = element_blank()

所以所有的情节都是一样的。您不会遇到不同纵横比的问题。您需要使用 R 或您最喜欢的图像编辑应用程序生成单独的 y 轴。

2.修复和尊重纵横比

aspect.ratio = 1 或您想要的任何比例添加到单个图的 theme()。然后在您的 respect=TRUE

中使用 grid.arrange()

通过这种方式,您可以将 y 轴保留在 plot1 中,并且在所有绘图中仍保持纵横比。灵感来自this answer

希望这些对您有帮助!