Ross D.
Ross D.

Reputation: 21

Creating a series of graphs in R

I have a series of three similar graphs that I would like to show in a single window in R..

This is the data:

Date Tank Time Female.in.Middle Female.in.R.assoc Female.in.L.assoc R.side.of.divider
117 25-Jun   I3  200           0.0966            0.2721            0.2001            0.0000
120 26-Jun   I3 1030           0.2118            0.5663            0.2212            0.0000
123 27-Jun   I3  200           0.0000            0.0000            0.0000            0.0000
128 28-Jun   I3 1100           0.0237            0.0672            0.1408            0.3289
129 28-Jun   I3  230           0.0683            0.0606            0.2488            0.0000
132 29-Jun   I3  200           0.1823            0.1180            0.6990            0.0000
135  2-Jul   I3  200           0.0000            0.0000            0.0000            0.0000
138  3-Jul   I3 1030           0.0000            0.0000            0.0000            0.0000
    L.side.of.Divider Tank.1 Date.Entered M.L.name Male.L.Length Male.L.Weight M.R.name
117            0.4285     I3       22-Jun  green 2           7.6         14.32   pink 1
120            0.0000     I3       22-Jun  green 2           7.6         14.32   pink 1
123            0.9961     I3       22-Jun  green 2           7.6         14.32   pink 1
128            0.4423     I3       22-Jun  green 2           7.6         14.32   pink 1
129            0.6215     I3       22-Jun  green 2           7.6         14.32   pink 1
132            0.0000     I3       22-Jun  green 2           7.6         14.32   pink 1
135            0.9952     I3       22-Jun  green 2           7.6         14.32   pink 1
138            0.9948     I3       22-Jun  green 2           7.6         14.32   pink 1
    Male.R.Length Male.R.Weight Side.of.Spawn F.Length F.Weight F.name last.female.spawn.date
117           7.7         14.79             L      5.5     5.64     2c                 22-Jun
120           7.7         14.79             L      5.5     5.64     2c                 22-Jun
123           7.7         14.79             L      5.5     5.64     2c                 22-Jun
128           7.7         14.79             L      5.5     5.64     2c                 22-Jun
129           7.7         14.79             L      5.5     5.64     2c                 22-Jun
132           7.7         14.79             L      5.5     5.64     2c                 22-Jun
135           7.7         14.79             L      5.5     5.64     2c                 22-Jun
138           7.7         14.79             L      5.5     5.64     2c                 22-Jun
    spawn.date.in.paradigm  X d.in.p dbs X.1 X.2 X.3
117                  3-Jul 11      3   8  NA  NA  NA
120                  3-Jul 11      4   7  NA  NA  NA
123                  3-Jul 11      5   6  NA  NA  NA
128                  3-Jul 11      6   5  NA  NA  NA
129                  3-Jul 11      6   5  NA  NA  NA
132                  3-Jul 11      7   4  NA  NA  NA
135                  3-Jul 11     10   1  NA  NA  NA
138                  3-Jul 11     11   0  NA  NA  NA

And I am using this code to make the graphs:

d2c<-plot(jd2c$d.in.p, jd2c$L.side.of.Divider, type='l', col='purple', xlab='Days After Entry', ylab='Proportion of Time Spent on Each Side of Divider', main='Female 2c', ylim=c(0,1))
legend('topleft',.8,c('Left', 'Right'),pch=c(.8), col=c('purple','green'))
points(jd2c$d.in.p, jd2c$R.side.of.divider, type='l', col='green')


az2c<-plot(jd2c$d.in.p, jd2c$Female.in.L.assoc, type='l', col='purple', xlab='Days After Entry', ylab='Proportion of Time Spent in Assoc. Zones', main='Female 2c', ylim=c(0,1))
legend('topleft',.8,c('Left', 'Right'),pch=c(.8), col=c('purple','green'))
points(jd2c$d.in.p, jd2c$Female.in.R.assoc, type='l', col='green')


rside2c<-cbind(jd2c$Female.in.R.assoc + jd2c$R.side.of.divider)
lside2c<-cbind(jd2c$Female.in.L.assoc + jd2c$L.side.of.Divider)

side2c<-plot(jd2c$d.in.p, lside2c, type='l', col='purple', xlab='Days After Entry', ylab='Proportion of Time Spent on Each Side (cumulative)', main='Female 2c', ylim=c(0,1))
legend('topleft',.8,c('Left', 'Right'),pch=c(.8), col=c('purple','green'))
points(jd2c$d.in.p, rside2c, type='l', col='green')

How can I get all three graphs to show up on one window?

Thank you!

Upvotes: 0

Views: 230

Answers (3)

Greg Snow
Greg Snow

Reputation: 49640

Others have mentioned par with mfrow, you can also use the layout function if you want more control over the position, size, and shape of the plotting regions.

Upvotes: 0

John
John

Reputation: 23758

It's unclear how you'd like the three graphs to appear but a simple solution is to preface your graphing with...

par(mfrow = c(1,3))

Now you'll see all three graphs in one window. You'll need to make the window wider of course. Otherwise the graphs will be all squished. You'll need to look up ?Devices for the particular output you want. If you just want it to appear on screen then X11 on Linux or quartz on Mac OS X will work. For example, on a Mac call...

quartz('my new graph', 15, 5)

That will make a space to draw in 3x as wide as it is high. It's automatically implicitly called with dimensions of 7" whenever you make an initial plot call.

You should probably look up par in help. It's also good advice to save the current settings and then restore them later. So, before graphing use...

opar <- par(mfrow = c(1,3))

and then after your graphing

par(opar)

That's an easy way to restore things even after you've made many changes to the graphing parameters.

Upvotes: 0

eykanal
eykanal

Reputation: 27017

You'll want to use par:

# Put this before your plots:
par(mfrow=c(3,1))  # your plots appear in one column, three rows,
                   # in one figure.
plot(...)
plot(...)
plot(...)
par(mfrow=c(1,1))  # always a good idea to return it to how it was
                   # afterwards, so later plots aren't affected

Upvotes: 1

Related Questions