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

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Aug 25 09:24:52 CEST 2010


On Wed, 2010-08-25 at 07:12 +0800, elaine kuo wrote:
> Dear List,
> 
> I have a richness data distributing across 20 N to 20 S latitude. (120 E-140
> E longitude).
> 
> 
> I would like to draw the richness in the north hemisphere and a regression
> line in the plot
> (x-axis: latitude, y-axis: richness in the north hemisphere).
> The above demand is done using plot.
> 
> Then, south hemisphere richness and regression are required to be generated
> using
> the same y-axis above but an x-axis on the left side of the y-axis.
> (The higher latitude in the south hemisphere, the left it would move)

Elaine,

It is not very clear what it is that you want here. Two plot panels on
the same device like:

layout(1:2)
plot(1:100)
plot(1:100)
layout(1)

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))

i.e. back to front to the normal axis limits? If so, you'll need to
produce a regression model for southern richness just as you would for
northern richness, but then predict from it at a sequence of latitude
values that covers the range of your southern hemisphere observations,
then plot these predictions versus the sequence of latitudes using
lines().

For example, say you have:

set.seed(123)
rich <- data.frame(richness = rpois(100, lambda = 5),
                   latitude = sample(seq(0, 90, length = 1000),
                                          100))
plot(richness ~ latitude, data = rich, xlim = c(90,0))

## ignoring that this is a count and might be more meaningfully
## modelled using a Poisson GLM
mod <- lm(richness ~ latitude, data = rich)

## new prediction data, 100 equally spaced latitudes
pdat <- data.frame(latitude = seq(0, 90, length = 100))
pdat <- within(pdat, richness <- predict(mod, newdata = pdat))
lines(richness ~ latitude, data = pdat, col = "red")

If that doesn't help, we'll need more info.

HTH

G

> Please kindly share how to design the south plot and regression line for
> richness.
> Also, please advise if any more info is in need.
> 
> Elaine
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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