.:. Megarhyssa .:.

A place for me to post pictures for family and friends, plug the odd interesting Web page, and discuss issues that are on my mind.

Thursday, February 08, 2007

Plotting degrees in R

Recently, I came across the problem of wanting to plot x values that were geographical coordinates (that is, I wanted to include degrees on the axis, or in the x axis label). After some puzzling over the issue (recall that I'm an R noob), I came across a query and answer on the R-help mailing list. Extremely useful as I found it, it lacked one element: the case in which you want to plot your own data, and only use the "at" part of the solution for creating labelled tick marks. Partially for my own benefit, I've added to the code suggested by Mark Schwartz, and I hope that somebody out there finds this helpful. Incidentally, this should be taken as an advertisement for R - scarcely a month after first picking up the package, I'm confident enough to edit and create code that produces publication-quality graphics.

# Create vector of x values for which you want degree labels
at <- seq(49, 54, 1)

# Create data to be plotted (normally, you will have data from
# columns in a spreadsheet)

x <- c(49, 50, 51, 52, 53, 53.8)
y <- c(1000, 1300, 1500, 1400, 1350, 1050)

# Create part of plot
plot(x, y, xlim = range(at), xaxt = "n", xlab = "Latitude", ylab = "Elevation (m a.s.l.)")

# Now create a set of plotmath expressions, one for
# each value of 'at' using paste() and parse()
# The general format for the degrees symbol is x*degree
# However, to add the "N" we use the "~" to create
# a right and left hand side for the expression and
# place the "N" after it. The "~" will not print.

L <- parse(text = paste(at, "*degree ~ N", sep = ""))

# Now do the x axis
axis(1, at = at, labels = L)

0 Comments:

Post a Comment

<< Home