[R] how to plot y-axis on the right of x-axis

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Aug 26 09:16:35 CEST 2010


On Thu, 2010-08-26 at 06:19 +0800, elaine kuo wrote:
> Yes, I appreciated your answers which well hit my questions. (esp the
> perfect model parts).
> 
> 
> About the plot part, one more question.
> Is it possible to make the two plots (northern and southern richness)
> sharing the same Y axis (latitude = 0, the equator) ?
> In other words, southern richness would be on the left side of the Y
> axis, 
> while northern one on the right side.

Yes, of course:

op <- par(mar = c(5.1,0,4.1,0), oma = c(0,4.1,0,4.1))
layout(matrix(1:2, nrow = 1))
plot(1:100)
plot(1:100, xlim = c(100,1), axes = FALSE)
axis(1)
axis(2, labels = FALSE, tcl = -0.5)
axis(2, labels = FALSE, tcl = 0.5)
axis(4)
box()
layout(1)
par(op)

Is one way doing things by hand. Notice the problem (or potential
problem) of having an axis in the middle of your data; it might obscure
data and the problem is exacerbated if you try to label it. Plus it is a
faff to fiddle around with the plotting margins, etc.

Can't you just code southern latitudes as -degrees N (minus degrees N)
and then plot all the data on the one plot, and fit your models to this
data rather that the original? After all, does it matter if you are at
0.1 degrees N or 0.1 degrees S and as such are in different hemispheres?
You are likely in the same biogeographical region. Here is an exmaple:

## dummy data
set.seed(123)
dat <- data.frame(richness = c(rpois(100, lambda = 5),
                               rpois(100, lambda = 2)),
                  latitudeN = c(sort(sample(seq(0.01, 90,
                                                length = 1000),
                                            100)),
                                sort(sample(seq(-0.01, -90, 
                                                length = 1000),
                                            100))))

## fit model to N sites:
## ignoring that richness is a count:
## hint use glm(.... family = poisson) as a start
modN <- lm(richness ~ latitudeN, data = dat, subset = latitudeN > 0)
## fit model to S sites
modS <- lm(richness ~ latitudeN, data = dat, subset = latitudeN < 0)

# prediction data
pdatN <- data.frame(latitudeN = seq(0.01, 90, length = 100))
pdatS <- data.frame(latitudeN = seq(-0.01, -90, length = 100))

## predict
pdatN <- within(pdatN, richness <- predict(modN, newdata = pdatN))
pdatS <- within(pdatS, richness <- predict(modS, newdata = pdatS))

## plotting
plot(richness ~ latitudeN, data = dat, 
     xlab = expression(Latitude~(degree*N)),
     ylab = "Species Richness")
lines(richness ~ latitudeN, data = pdatN, col = "red")
lines(richness ~ latitudeN, data = pdatS, col = "blue")
legend("topleft", ncol = 2, legend = c("N","S"),
       col = c("red","blue"), lty = "solid", bty = "n")

HTH

G

> Elaine
> 
> 
> 
> 
> Two plot panels on the same device like:
> 
> layout(1:2)
> plot(1:100)
> plot(1:100)
> layout(1)
> 
> 
> =>  Yes that's what I want
> 
> The second panel for southern species richness, do you mean you want
> the
> plot to go like this:
> 
> plot(1:100, xlim = c(100,1))
> 
> 
> =>  Yes, too. 
> 
> 
> 
> 
> 
> 

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list