1

I'm creating two different x,y plots with ggplot2. I want the scale of the y-axis to be the same (e.g. in both plots the distance between 0 and 10 is the same) but the length of each figure/plot to be different (if one plot goes to 20, and the other goes to 50, the the one that goes to 50 should be larger).

facet_Grid isn't a great option as I have more than a hundred different plots to make.

Example data:

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

Plot 1: y column data ranges from 0 to 20. x scale is 0 to 100
Plot 2: y column data ranges from 0 to 60. x scale is 0 to 100

Standard plot:

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

I get two plots of the same size, but the scale is different between them on the y-axis.

If I put limits on y to force them to be the same:

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)

The first plot has a ton of wasted space between 20 and 60 that I don't want.

Any ideas of how to keep a consistent scale size in an output image, while having a different axis limit (thus different plot height in this case)? I need them to have the same scale on the printed page, but different heights.

Any ideas? Playing with scale_y_continuous, coord_equal, etc hasn't really managed to get me anywhere.

Example photoshopped mock-up of what I'm trying to make, but as separate output files. In each figure, 1cm on the y-axis would be the same between the multiple plots:

enter image description here

mat4nier
  • 262
  • 4
  • 14
  • 1
    Please read the info about [how to ask a question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Also share any code that you’ve tried so far. This will make it much easier for others to help you. – Jaap May 28 '14 at 18:33
  • probable duplicate of http://stackoverflow.com/q/23906277/471093 – baptiste May 28 '14 at 18:42
  • Updated with code trying to better explain this. I need the same scale on the graphs, but different sized figures. – mat4nier May 28 '14 at 18:48
  • @rockandrcrush this should be closed/accepted/deleted if you've solved and or specified your problem. – npjc May 12 '15 at 09:15

1 Answers1

2

Looking for something like this perhaps?

some data

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

combine and add reference col df

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

add a new column of group-wise maximum y value with dplyr

require(dplyr)
newdf <- newdf %>% group_by(df) %>% mutate(ymax = max(y)) 

Now we plot both our line data and a scaling boundary with 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

Finally, use a theme to get rid of some elements...

An example a very limited theme object called 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)

adding this to our plot:

 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

gives:

second_go

npjc
  • 4,134
  • 1
  • 22
  • 34
  • Trying to move away from facet_grid due to the number of plots. The example shows 2 plots. In reality I have ~150 that I need at the same scale. In the above example, I'd like df2 to have all the whitespace removed from 20-60 (e.g. the scale would be the same, but the image height would be much shorter). – mat4nier May 28 '14 at 18:59
  • @rockandrcrush for future ref make your post informative about any limitations possible solutions need to adhere to. see: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – npjc May 28 '14 at 19:03
  • updating the post as fast as I can. – mat4nier May 28 '14 at 19:07