# [R] plotting on a reverse log scale

Marc Schwartz MSchwartz at mn.rr.com
Thu Jul 7 04:11:48 CEST 2005

```I thought that I would take a stab at this. I should note however that
my solution is heavily biased by the first log scale plot that Michael
referenced below. To wit, my x axis has major ticks at powers of 10 and
minor ticks at 1/10 of each major tick interval.

Thus, here is my approach:

# Set the max range to an integer power of 10 as may be required
max.pow <- 5

# Set the data as others have done below
set.seed(1)
x <- runif(500, 1, 10^max.pow)
d <- density(x, from = 1, to = 10^max.pow)

# Now do the density plot, leaving the x axis blank
# Note that by doing a rev() on 'xlim' it reverses the min/max on the
# x axis, so you don't have to worry about adjusting things.
plot(d\$y ~ d\$x, xlim = rev(c(1, 10^max.pow)), log = "x",
xaxt = "n", type = "l",
xlab = "2000 - Year (log scale)")

# Set the axis major tick marks
axis.at <- 10 ^ c(0:max.pow)

# Draw the major tick marks and label them using plotmath
axis(1, at = axis.at, tcl = -1,
labels = parse(text = paste("10^", 0:max.pow, sep = "")))

# Now do the minor ticks, at 1/10 of each power of 10 interval
axis(1, at = 1:10 * rep(axis.at[-1] / 10, each = 10),
tcl = -0.5, labels = FALSE)

# Do the rug plot
rug(x)

Not sure if this is helpful here, but thought I would post it for
review/critique. The 'max.pow' constant can be explicitly adjusted or
can be calculated automatically based upon input year ranges.

Best regards,

Marc Schwartz

On Wed, 2005-07-06 at 17:31 -0400, Gabor Grothendieck wrote:
> Not sure if I am missing something essential here but it would
> seem as simple as:
>
> # data
> set.seed(1)
> x <- runif(500, 1500, 1990)
>
> # plot
> d <- density(x, from = 1500, to = 1990)
> plot(d\$y ~ d\$x, log = "x")
> rug(x)
> axis(1, seq(1500, 1990, 10), FALSE, tcl = -0.3)
>
>
> On 7/6/05, Michael Friendly <friendly at yorku.ca> wrote:
> > Thanks Duncan,
> > That is almost exactly what I want, except I want time to
> > go in the normal order, not backwards, so:
> >
> > # plot on reverse log scale
> > years1500 <- runif(500, 1500, 1990)  # some fake data
> > x <- -log(2000-years1500)
> > from <- -log(2000-1990)
> > to <- -log(2000-1500)
> > plot(density(x, from=from, to=to), axes=F)
> > rug(x)
> >
> > labels <- pretty(years1500)
> > labels <- labels[labels<2000]
> > axis(1, labels, at=-log(2000-labels))
> >
> > minorticks <- pretty(years1500, n=20)
> > minorticks <- minorticks[minorticks<2000]
> > axis(1, labels=FALSE, at=-log(2000-minorticks), tcl=-0.25)
> >
> > axis(2)
> > box()
> >
> > -Michael
> >
> > Duncan Murdoch wrote:
> >
> > > On 7/6/2005 3:36 PM, Michael Friendly wrote:
> > >
> > >> I'd like to do some plots of historical event data on a reverse log
> > >> scale, started, say at the year 2000 and going
> > >> backwards in time, with tick marks spaced according to
> > >> log(2000-year).  For example, see:
> > >>
> > >> http://euclid.psych.yorku.ca/SCS/Gallery/images/log-timeline.gif
> > >>
> > >> As an example, I'd like to create a density plot of such data with the
> > >> horizontal axis reverse-logged,
> > >> a transformation of this image:
> > >> http://euclid.psych.yorku.ca/SCS/Gallery/milestone/Test/mileyears1.gif
> > >>
> > >> Some initial code to do a standard density plot looks like this:
> > >>
> > >> mileyears <- read.csv("mileyears3.csv", skip=1,
> > >> mileyears <- mileyears[,2:4]
> > >>
> > >> years <- mileyears\$year
> > >> years1500 <- years[years>1500]
> > >> dens <- density(years1500, from=1500, to=1990)
> > >> plot(dens)
> > >> rug(years1500)
> > >>
> > >> I could calculate log(2000-year), but I'm not sure how to do the
> > >> plotting, do some minor tick marks
> > >> and label the major ones, say at 100 year intervals.
> > >
> > >
> > > I think you'll have to do everything explicitly.  That is, something
> > > like this:
> > >
> > > years1500 <- runif(500, 1500, 1990)  # some fake data
> > > x <- log(2000-years1500)
> > > from <- log(2000-1990)
> > > to <- log(2000-1500)
> > > plot(density(x, from=from, to=to), axes=F)
> > > rug(x)
> > >
> > > labels <- pretty(years1500)
> > > labels <- labels[labels<2000]
> > > axis(1, labels, at=log(2000-labels))
> > >
> > > minorticks <- pretty(years1500, n=20)
> > > minorticks <- minorticks[minorticks<2000]
> > > axis(1, labels=FALSE, at=log(2000-minorticks), tcl=-0.25)
> > >
> > > axis(2)
> > > box()
> > >

```