1

Is there a way to force

align.plots()

(ggExtra) to produce a plot where the "individual" y-axis are of the same height/length?

Plot1:

kurth.plot <- qplot(x=trade.date, y=kurth, data=finalregstack, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), plot.margin = unit(c(1,0,0,0), "lines"))

Plot2:

skewh.plot <- qplot(x=trade.date, y=skewh, data=finalregstack, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(),plot.margin = unit(c(-1,0,0,0), "lines"))

Plot2:

logpremh.plot <- qplot(x=trade.date, y=logpremh, data=finalregstack, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), plot.margin = unit(c(-1,0,0,0), "lines"))

All from the same data frame (finalregstack), with trade.date one the x-axis.

align.plots(kurth.plot, skewh.plot, logpremh.plot)

returns at plot where the y-axis are of different height. Would like to have three plots with the same size, and ticks and axis.text on all the x-axis...

finalregstack:

structure(list(trade.date = structure(c(11869, 11870, 11871, 
11876, 11877, 11878, 11879, 11880, 11883, 11884, 11885, 11886, 
11887, 11890, 11891, 11892, 11893, 11894, 11897, 11898, 11899, 
11900, 11901, 11904, 11905, 11906, 11907, 11908, 11911, 11912, 
11913, 11914, 11915, 11918, 11919, 11920, 11921, 11922, 11926, 
11927, 11928, 11929, 11933, 11934, 11935, 11936, 11939, 11940, 
11941, 11942, 11943, 11946, 11947, 11948, 11949, 11950, 11953, 
11954, 11955, 11956, 11957, 11960, 11961, 11962, 11963, 11964, 
11967, 11968, 11969, 11970, 11971, 11974, 11975, 11976, 11977, 
11978, 11981, 11982, 11983, 11984, 11985, 11988, 11989, 11990, 
11991, 11992, 11995, 11996, 11997, 11998, 11999, 12002, 12003, 
12004, 12005, 12006, 12009, 12010, 12011, 12012), class = "Date"), 
    kurth = c(-0.538313927970605, -0.489599831389002, -0.549781828821448, 
    -0.471480517120967, -0.77047442140922, -0.764965969697188, 
    -0.797709358466937, -0.832263316808549, -0.739745115199957, 
    -0.737998397925412, -0.67595581513572, -0.765405764054348, 
    -0.901490974527926, -0.89225716170503, -0.939998163588295, 
    -0.758003571622454, -0.947725995002572, -1.00684152882085, 
    -1.00573304351807, -0.934273517386361, -0.859399051665602, 
    -0.942893064943436, 2.47305763864541, 2.06181470542550, 1.56582316347337, 
    1.19831125493319, 1.22311352859957, 1.15155464086347, 8.89610455063215, 
    8.86956391322178, 8.68580530405464, 8.61123904123055, 8.7400669250269, 
    8.79737625016586, 8.78629168919513, 8.29623941128428, 8.05754059674136, 
    8.02077532530273, 8.23011985866437, 8.24401447203093, 7.92294576051403, 
    7.4281887928507, 7.3426056371313, 7.30433477318178, 7.32670525202146, 
    7.17139705328142, 7.25310778701676, 7.1313581631237, 6.90092496220942, 
    6.81977869235276, 8.7728390134954, 9.0300473239695, 9.86979682886692, 
    10.0467584323277, 9.18063174440517, 9.60705497699159, -0.834707723641238, 
    -0.831476742135092, -0.683003538196646, -0.128689206047591, 
    -0.0486840773604658, -0.193928790670311, -0.372070480951705, 
    1.24156083387007, 2.16386195607461, 1.94481670179387, 1.87512691616447, 
    1.60466257254115, 1.58162121662634, 1.82406671267336, 2.14439006082267, 
    2.11326738777539, 1.84632151012893, 1.81703000912427, 1.70683982987174, 
    1.52156804636807, 1.37669564606729, 1.34356392851010, 1.38192685184208, 
    0.979515684470376, 0.888891137016543, 0.675602543012428, 
    0.661040087943164, 0.806562602010497, 1.03315305152296, 0.78156186596084, 
    0.8053103693573, 0.826531073748131, 0.825522225795727, 0.898444393507555, 
    1.10994069195078, 1.10994069195078, 1.10994069195078, 1.1364451896438, 
    1.09982566285141, 1.85866580040354, -0.351810796009023, -0.096669160154403, 
    -0.492681631027265, -0.288429330247108), skewh = c(0.558228477951341, 
    0.58308901843335, 0.609602375964546, 0.477880650885013, 0.387805408324395, 
    0.387839989209099, 0.286456452315072, 0.193331879936383, 
    0.206591453689309, 0.0971220953017504, 0.221147814384501, 
    0.187595125737881, 0.0524187756954932, 0.0961716860713602, 
    0.182338727065898, 0.264757353490630, 0.163830324947476, 
    0.143991871613884, 0.144463923069265, 0.0730550579677163, 
    0.183757658876955, 0.0656791648713786, 1.18950139112842, 
    1.09434539636253, 0.802846925891693, 0.63602979484928, 0.672813293573144, 
    0.736614149819276, -2.33563391004889, -2.31143640895283, 
    -2.31727166871707, -2.32876897713447, -2.38781198320570, 
    -2.37160833347724, -2.33074692187816, -2.24219936843415, 
    -2.21001773133659, -2.17823730104347, -2.2105667535483, -2.22156866044437, 
    -2.19942975461446, -2.07253305889856, -2.0419313498378, -2.02266448996358, 
    -1.97786014003463, -1.95408902307469, -1.98284250481692, 
    -1.93088774946092, -1.92119697583021, -1.88750434547032, 
    -2.45890556022003, -2.53154407812213, -2.67753652485642, 
    -2.70481025800469, -2.54591006187052, -2.62989769761383, 
    0.247330549690984, 0.228815399584095, 0.227991391662379, 
    0.571885460253403, 0.689877958381067, 0.533755075074511, 
    0.400111197818491, -0.535379813307516, -1.12355112329518, 
    -1.11402405119459, -1.06419661476207, -1.01542648507456, 
    -0.98798275182458, -1.01968536538631, -1.13136880338172, 
    -1.11658585003117, -1.08702903755025, -1.07310996361807, 
    -1.04443252316148, -0.98146201617556, -0.989718695652915, 
    -0.978648824220157, -0.93936499984734, -0.842000316650154, 
    -0.856125603005716, -0.752318760987758, -0.743850925871073, 
    -0.847638348832287, -0.918625055313669, -0.846416731547665, 
    -0.864256826872963, -0.890095785952014, -0.892616437941412, 
    -0.867123449112882, -0.936966060328, -0.936966060328, -0.936966060328, 
    -0.758598109998537, -0.682579399329156, -0.619814484766827, 
    0.620814547041089, 0.725657661522822, 0.592805261790067, 
    0.721071190789906), logpremh = c(-0.995610217466355, -0.70000069009059, 
    -0.678144103868504, -0.678144103868504, -0.494505232880775, 
    -0.484213835616396, -0.495268057973604, -0.372900957639147, 
    -0.450480847761974, -0.437795043597507, -0.517585169818772, 
    -0.430739184642212, -0.415173125239592, -0.313292951895139, 
    -0.552202213743963, -0.646341796497822, -0.715263921701793, 
    -0.660538105883145, -0.64253792833408, -0.491943429630031, 
    -0.605111281016567, -0.687871516304753, -0.819398672581242, 
    -0.720843146615785, -0.554697314678323, -0.604358822116492, 
    -0.635963319554632, -0.58688762990502, -0.533766579673723, 
    -0.549540818233004, -0.509744651370597, -0.497574488575739, 
    -0.50295497641327, -0.465150684081682, -0.471107502191422, 
    -0.484308973579098, -0.519413594601966, -0.409658720851543, 
    -0.409658720851543, -0.312434559177703, -0.240594651315486, 
    -0.187647447055703, -0.187647447055703, -0.431835237434803, 
    -0.492239335398449, -0.601873889653004, -0.847778497243267, 
    -1.28468760005568, -1.34064083106371, -1.32738794981778, 
    -1.43946408093213, -1.66269758662786, -1.56874565640878, 
    -1.68389571629343, -1.57211119569467, -1.49267822488204, 
    -1.42922611404924, -1.74337835169184, -1.75526498666775, 
    -1.65718139709817, -1.49798628913073, -1.3162509752857, -1.25568570523457, 
    -1.32158185197453, -1.32751736653430, -1.79028679759309, 
    -1.68133286507375, -1.59210428304230, -0.358448286780251, 
    -0.282320721989257, -0.222935326576976, -0.140064751669158, 
    -0.0652636984938439, -0.0843461362304372, -0.065141838860399, 
    -0.0424518787941153, 0.0550573562398911, -0.190765741146921, 
    -0.269701195073448, -0.173665980472658, -0.0334693122151012, 
    -0.539434660595658, -0.376468992885012, -0.446138926996241, 
    -0.368295712776361, -0.203379346763717, -0.123717788393908, 
    -0.0460878584714188, -0.463198358966021, -0.726046498313927, 
    -1.00170780286485, -0.831902448806796, -0.804806611101685, 
    -0.787069116472282, -0.97805437572716, -0.908526799872866, 
    -0.735307225195469, -0.696862274769597, -0.635797410804945, 
    -0.514329954731469)), .Names = c("trade.date", "kurth", "skewh", 
"logpremh"), row.names = c("2002-07-01", "2002-07-02", "2002-07-03", 
"2002-07-08", "2002-07-09", "2002-07-10", "2002-07-11", "2002-07-12", 
"2002-07-15", "2002-07-16", "2002-07-17", "2002-07-18", "2002-07-19", 
"2002-07-22", "2002-07-23", "2002-07-24", "2002-07-25", "2002-07-26", 
"2002-07-29", "2002-07-30", "2002-07-31", "2002-08-01", "2002-08-02", 
"2002-08-05", "2002-08-06", "2002-08-07", "2002-08-08", "2002-08-09", 
"2002-08-12", "2002-08-13", "2002-08-14", "2002-08-15", "2002-08-16", 
"2002-08-19", "2002-08-20", "2002-08-21", "2002-08-22", "2002-08-23", 
"2002-08-27", "2002-08-28", "2002-08-29", "2002-08-30", "2002-09-03", 
"2002-09-04", "2002-09-05", "2002-09-06", "2002-09-09", "2002-09-10", 
"2002-09-11", "2002-09-12", "2002-09-13", "2002-09-16", "2002-09-17", 
"2002-09-18", "2002-09-19", "2002-09-20", "2002-09-23", "2002-09-24", 
"2002-09-25", "2002-09-26", "2002-09-27", "2002-09-30", "2002-10-01", 
"2002-10-02", "2002-10-03", "2002-10-04", "2002-10-07", "2002-10-08", 
"2002-10-09", "2002-10-10", "2002-10-11", "2002-10-14", "2002-10-15", 
"2002-10-16", "2002-10-17", "2002-10-18", "2002-10-21", "2002-10-22", 
"2002-10-23", "2002-10-24", "2002-10-25", "2002-10-28", "2002-10-29", 
"2002-10-30", "2002-10-31", "2002-11-01", "2002-11-04", "2002-11-05", 
"2002-11-06", "2002-11-07", "2002-11-08", "2002-11-11", "2002-11-12", 
"2002-11-13", "2002-11-14", "2002-11-15", "2002-11-18", "2002-11-19", 
"2002-11-20", "2002-11-21"), class = "data.frame")
fredrik
  • 41
  • 5
  • 1
    Yes there is. But you will make it a **lot** easier to answer your questions if you post some reproducible code in your question: http://stackoverflow.com/q/5963269/602276 This will also make it easier for us to evaluate whether you *really* need `align.plots()` and whether a simple facetting wouldn't be more appropriate. – Andrie May 26 '11 at 12:21
  • That's better. Now provide a sample data.frame called finalreg. The code needs to be reproducible, i.e. I should be able to paste it into my console and it will produce the graph you see. (And use line breaks and proper code formatting so that we don't have to scroll to the right to see all of the code.) – Andrie May 26 '11 at 12:50
  • It looks very much like this can be done by melting and faceting ... – Ben Bolker May 26 '11 at 13:00

2 Answers2

1

How about

library(reshape2)
finalregstack$trade.date <- as.Date(rownames(finalregstack))
fm <- melt(finalregstack,id.var="trade.date")

library(ggplot2)
qplot(trade.date,value,data=fm,geom="line")+
  facet_grid(variable~.,scale="free_y")+
  labs(x="Trading date",y="")+
  theme_bw() ## cosmetic

updated using reshape2 and scale="free_y"

The only real problem with this solution (which is fairly difficult to fix) is that the individual y-axis labels come out as strips on the right, not as traditional axis labels on the left. If you can convince your readers to look on the left ...

If you really need to do this via align.plot (because you need the ticks etc.), the most recent version of align.plots (which I grabbed from the googlecode repository) has a heights argument. I started to mess around with this but haven't got it working to my liking yet ...

library(ggExtra)

theme_set(theme_bw())
theme_update(panel.background=theme_rect(),
              panel.grid.major=theme_blank(),
             panel.grid.minor=theme_blank())

kurth.plot <- qplot(x=trade.date, y=kurth,
                    data=finalregstack, geom='line')+
  scale_y_continuous(name='kurth')+
  scale_x_date(name='',expand=c(0,0))+
  opts(plot.margin = unit(c(1,0,0,0), "lines"))

skewh.plot <- qplot(x=trade.date, y=skewh,
                    data=finalregstack, geom='line')+
  scale_y_continuous(name='skewh')+
  scale_x_date(name='', expand=c(0,0))+
  opts(plot.margin = unit(c(-1,0,0,0), "lines"))


logpremh.plot <- qplot(x=trade.date, y=logpremh,
                       data=finalregstack, geom='line')+
  scale_y_continuous(name='logpremh')+
  scale_x_date(name='Trading date', expand=c(0,0))+
  opts(plot.margin = unit(c(-1,0,0,0), "lines"))


source(url("http://ggextra.googlecode.com/svn/trunk/R/align.r"))
grid.newpage()
align.plots(kurth.plot, skewh.plot, logpremh.plot,
            heights=unit(c(0.33,0.33,0.33),"null"))

By the way: since all your plots have the same x axis, is there a possibility that grid.arrange would work for you? align.plots is designed for fussier situations (e.g. some plots have strips/legends/etc. and others don't).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Snap. You beat me by seconds. (But then, I also included the graph.) PS. You can get a performance increase by using `library(reshape2)` – Andrie May 26 '11 at 13:51
  • trying to get ticks with text on all the x-axis with facet_grid, you have any ideas? – fredrik May 26 '11 at 15:20
  • @Ben, tried grid.arrange, but I it does not align the graphs and they are still different sizes. Any way I can set the height in opts or scale_y? – fredrik May 26 '11 at 19:02
  • see above -- you can set custom/adjustable heights in the newest version of `align.plots`. The heights above are not correct, but I think you should be able to get them equal by fussing (there may be a non-fussy/automatic solution but I don't know it). – Ben Bolker May 26 '11 at 19:09
  • @Ben, tried the heights=unit(c(0.33,0.33.033), 'null'), but the top graph is still smaller, don't understand why? – fredrik May 26 '11 at 19:12
  • I don't understand why either -- I think it may have to do with varying margin sizes etc. What we really want is for plot *regions* to be the same size ... but in lieu of having a good way to do that, I would just use trial-and-error adjustment of the heights until it looked good. If you need to do this on a regular basis with different kinds of plots it may be worth some hacking. (I hacked the `heights` argument myself because I needed it.) It may be worth asking Baptiste Auguie and Hadley W. whether a new/redesigned version of `ggplot` that will make this stuff easier is on the horizon. – Ben Bolker May 26 '11 at 19:14
  • thanks for the help Ben, I will try some trail and error, or just convince my professor that facet_grid is a better way to present the graphs. – fredrik May 26 '11 at 19:28
  • @Ben, setting the same plot.margin=unit(c(0,0,-1,0) is the solution. than using heights=unit(c(0.33,0.33,0.33),'null'). Thanks again! – fredrik May 26 '11 at 19:44
  • Glad it worked -- although now I wonder if these differential margins were the source of your problem all along (not that it isn't nice to be able to adjust the heights). Do you get the right plot if you set the margins identical and then use `align.plots` without any height adjustment ... ? – Ben Bolker May 26 '11 at 19:49
1

Personally, I would use facetting to solve this, not aligning plots. This means you can let ggplot decide what the y-axis values should be. It also makes for much more readable code

library(reshape2)

finalregstack$trade.date <- 1:100
mregstack <- melt(finalregstack, id.vars="trade.date")

ggplot(mregstack, aes(x=trade.date, y=value, colour=variable)) + geom_line() +
    facet_grid(variable~.) + theme_bw()

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • I think yours is a little better because you explain why. If I were going to amalgamate these solutions I would (1) skip the colouring of the lines on yours [might as well save it for a case where it's necessary] (2) use my solution for dates (3) maybe fix up the x and y labels ... (4) we both forgot that we probably want to use `scale="free_y"` in this case – Ben Bolker May 26 '11 at 14:03
  • yepp have tried facet_grid, but need ticks with text (like on the graph on the "bottom") on all the x-axis. Can't find any way to do this with facet_grid. – fredrik May 26 '11 at 15:15
  • @fredrik, why? I am a big fan of Edward Tufte. He calls any extraneous ink on a chart "chart junk". In my view, adding additional tick will add nothing to this chart, and simply increase the chart junk ratio. – Andrie May 26 '11 at 15:37
  • true. I don't like it either, but have to please my professor... know how to do it? – fredrik May 26 '11 at 15:51
  • facet_wrap(~variable, ncol=1) maybe? – baptiste May 26 '11 at 20:45