图输出在图之间具有相同的比例尺寸,但轴长不同

时间:2014-05-28 18:19:36

标签: r ggplot2

我正在使用ggplot2创建两个不同的x,y图。我希望y轴的比例相同(例如,在两个图中0到10之间的距离是相同的)但每个图/图的长度是不同的(如果一个图变为20,另一个图到50,到50的那个应该更大)。

facet_Grid不是一个很好的选择,因为我有超过一百个不同的情节。

示例数据:

dat1 <- data.frame(replicate(2,sample(0:20,10,rep=TRUE)))
dat2 <- data.frame(replicate(2,sample(0:60,10,rep=TRUE))

图1:y列数据的范围为0到20.x标度为0到100
曲线2:y列数据的范围为0至60.x标度为0至100

标准情节:

ggplot(data = dat1, aes (x=X1, y= X2)) + xlim(0,100)
ggplot(data = dat2, aes (x=X1, y= X2)) + xlim(0,100)

我得到两个相同大小的图,但它们在y轴上的比例不同。

如果我对y施加限制以迫使它们相同:

ggplot(data = dat1, aes (x=X1, y= X2)) + xlim(0,100) + ylim(0,60)
ggplot(data = dat2, aes (x=X1, y= X2)) + xlim(0,100) + ylim(0,60)

第一块地块有20到60之间的浪费空间,我不想要。

如何在输出图像中保持一致的比例尺寸,同时具有不同的轴限制(在这种情况下,不同的绘图高度)?我需要它们在打印页面上具有相同的比例,但不同的高度。

有什么想法吗?使用scale_y_continuouscoord_equal等进行游戏并没有真正让我感到满意。

示例我试图制作的模型,但作为单独的输出文件。在每个图中,y轴上的1cm在多个图之间是相同的:

enter image description here

1 个答案:

答案 0 :(得分:2)

也许正在寻找这样的事情?

一些数据

df1 <- data.frame(x= rep(1:10,10), y = 1:100)

组合并添加参考col df

newdf <- rbind(data.frame(df1[1:60,],df="df1"),data.frame(df1[1:20,],df="df2"))

使用dplyr

添加一组新的分组最大y值
require(dplyr)
newdf <- newdf %>% group_by(df) %>% mutate(ymax = max(y)) 

现在我们使用geom_rect()

绘制线数据和缩放边界
ggplot(newdf) + 
    geom_rect(fill=NA,colour="grey",aes(xmin=0,xmax=max(x),ymin=0,ymax=ymax)) + 
    facet_grid(. ~ df, margins = FALSE) + geom_line(aes(x,y))

first_go

最后,使用主题摆脱一些元素......

一个名为thmEls的非常有限的主题对象示例。

require(grid)
require(ggplot2)

thmEls <- 
    theme(
        # PARENT ----------------------------------------------
        # line = element_blank(),
        rect = element_rect(fill="white",colour="gray40", size=0.35, linetype=1),
        # text = element_blank(),
        # title = NULL,
        # axis.title = NULL,

        # AXIS -------------------------------------------------
        # axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        # axis.text = element_blank(),
        # axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.ticks = element_blank(),
        # axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank(),
        # axis.ticks.length = element_blank(),
        # axis.ticks.margin = element_blank(),
        # axis.line = element_blank(),
        # axis.line.x = element_blank(),
        # axis.line.y = element_blank(),

        # LEGEND ----------------------------------------------
        # legend.background = element_blank(),
        # legend.key = element_blank(),
        # legend.key.size = element_blank(),
        # legend.key.width = element_blank(),
        # legend.text = element_blank(),
        # legend.text.align = element_blank(),
        # legend.position = element_blank(),
        # legend.direction = element_blank(),
        # legend.justification = element_blank(),
        # legend.box = element_blank(),
        # legend.box.justification = element_blank(),

        # PANEL ------------------------------------------------
        # ,size=0.3 from panel.background
        panel.background = element_blank(),
        # panel.border = element_blank(),
        panel.margin = unit(c(0.125),"lines"),
        # panel.grid = element_blank(),
        # panel.grid.major = element_blank(),
        # panel.grid.minor = element_blank(),
        # panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),

        # PLOT ------------------------------------------------
        plot.background = element_blank(),
        plot.title = element_blank(),
        plot.margin = unit(c(0.3,0.3,0.15,0.15),"lines"),

        # STRIP -----------------------------------------------
        strip.background = element_blank(),
        strip.text = element_blank(),
        # strip.text.x = element_blank(),
        # strip.text.y = element_blank(),
        complete = FALSE)

将此添加到我们的情节中:

 ggplot(newdf) + 
    geom_rect(fill=NA,colour="grey",aes(xmin=0,xmax=max(x),ymin=0,ymax=ymax)) + 
    facet_grid(. ~ df, margins = FALSE) + geom_line(aes(x,y)) + 
    thmEls

给出:

second_go

相关问题