ifreak
ifreak

Reputation: 1756

plot 4 different plots inside 1 pdf

I have 2 data frames which i want to generate 3 plots from them and place them inside 1 pdf file as a single column.

i want all the plots to have the same x-axis limits(basically the same x-axis) even thought they differ in the name and how they were obtained.

the dataframes looks something like that:

d1

     X          Y       Z
0.04939317 -0.4622222  13651
0.03202451 -0.4261000  13401
0.09950793 -0.3233025  13151
0.11548556 -0.4637981  12486
0.09817597 -0.4751886  12236
0.15770701 -0.5819355  11986

and d2

   V0  V1   V2    V3   sign
   1   1  0.379 0.612   pos
   2   1  0.378 0.620   pos
   3   1  0.578 0.571   neg
   4   1  0.978 0.561   pos
   5   1  0.758 0.261   neg
   6   1  0.378 0.126   neg

P.S : both data frames are bigger than this, this is only a part of them

V0, V1 and Z range from 1 to 20000

the plots that i created are :

From d2

d2plot=ggplot(d1, aes(V0,V1, fill=sign)) + 
    geom_tile()+ scale_fill_manual(values = c("neg" = "yellow", "pos"="red")) +
    geom_vline(xintercept =10000 ) +
    geom_text(mapping=aes(x=10000,y=0, label="Stop"), 
    size=4, angle=90, vjust=-0.4, hjust=0)

From d1

d1plot = ggplot(d2) + 
    geom_errorbarh(aes(x=z,xmin=z-50,xmax=z+50, y=Y, height = 0.02),
    color="red")+ opts(legend.position = "none")  +
    geom_vline(xintercept = 10000) +
    geom_text(mapping=aes(x=10000,y=-0.3, label="Stop"), 
    size=4, angle=90, vjust=-0.4, hjust=0)

I've tried grid.arrange(d1plot, d2plot, ncol=1) but the x-axis is different for each plot, i tried changing the aspect ratio, but this will change the y-axis ..I've also tried to use facet_wrap but the problem that my x-axis values have different values, i just want the limits and breaks to be the same and the plots all be aligned in 1 column based on 1 x-axis to compare the value of the statistical methods in an easy way.

Upvotes: 0

Views: 243

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

It should work if you set the x axis limits manually.

Add

+ coord_cartesian(xlim = c(1, 20000))

to each plot before combining them.


To achive different aspect ratios you can use

+ coord_fixed(xlim = c(1, 20000), ylim = c(1, 20000))

Change the ylim parameter for each plot to fit your actual data.

Upvotes: 1

Jochem
Jochem

Reputation: 3387

You can also safe each individual plot to PDF and then open them in Adobe Illustrator or Inkscape and copy the elements into one PDF. This will also give you the opportunity to pimp your charts a bit and add some additional explaination etc. On this website (http://flowingdata.com) there are a number of tutorials that help you along but the basis is really simple. Safe R plot to PDF and open in Illustrator and you can select all elements (and thus also copy and combine them). Good luck!

Upvotes: 1

Related Questions