1

My question is very similar to this question but I wish to write special values on the x-axis.
I have the following code:

x <- c(1:12)
y <- c(1:12)
filled.contour(c(2,4,7,10,14,21,30,60,90,120,180,365), y, outer(x,y))

Gives the following plot :enter image description here

My problem is that I don't want the plot to be squeezed on the left but want all values in c(2,4,7,10,14,21,30,60,90,120,180,365) to be equally spaced on the x-axis.

How can I do this?
As an extension, how can I put characters "two", "four", ... instead of 2, 4, ... on the x-axis?

Thanks

Community
  • 1
  • 1
user88595
  • 195
  • 9

1 Answers1

2

This is a start:

x <- 1:12
y <- 1:12
xvals <- c(2,4,7,10,14,21,30,60,90,120,180,365)
fx <- as.numeric(as.factor(xvals))
filled.contour(fx, y, outer(x,y),
               plot.axes= {
                   axis(2)  ## plain
                   axis(1,at=fx,labels=xvals)
               })

enter image description here

However, you can see that the '180' label is already getting omitted, which means that things are going to be even worse if you translate the x-axis labels into words (do you really want the last tick label to read "three hundred sixty-five")? (You can use par(las=3) to turn the labels vertical, but that's ugly too.)

This question points us to qdap::replace_number(), but it doesn't seem to work for single-digit numbers ...

 library(qdap)
 filled.contour(fx, y, outer(x,y),
                plot.axes= {
                    axis(2)  ## plain
                    axis(1,at=fx,labels=replace_number(xvals))
 })

update: the qdap maintainer has already fixed this bug; if you need the latest version you could try devtools::install_github("qdap","trinker")

enter image description here

If you're in a big hurry you could just fix the result by hand.

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Sorry for being so long. Great thanks! It works and I wrote "yearly" instead of "365" and so on. I gave `par(las = 3)` a try, indeed it's ugly and overlaps my x label. Is there a way I can make it diagonal? I tried other values of `las = ` but none seem to work. – user88595 May 25 '14 at 12:48
  • diagonal is harder, but if you look around you'll find it: googling "r diagonal axis text" got my several good hits, including a few on SO (actually this is an [R FAQ](http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f)) – Ben Bolker May 25 '14 at 13:47