R Plotting tips

[This article was first published on EvolvingSpaces, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Yes…I haven’t been updating the blog for a very long while…But I’m really busy with many things right know…. Our paper for the “Cultural Evolution in Spatially Structured Populations” have been accepted so we are currently working on that, and I have also stuffs for the PhD and a couple of papers I’m working on…busy busy busy…..
I’ve also started writing a couple of ABM using R, which sounds crazy at first (also at second, and third) but it has some nice things which I’ll write about extensively in a future post.
But for know, I just wanted to start a series of very small posts (mainly for archaeologists) of small tips, which are astonishing simple concepts which however takes a couple of hours of googling and forum foraging…
For instance, have you ever tried to plot a time series of BC or BP dates? Suppose you have a sequence of count per century as follows:

data<-c(789,100,923,444,224,192,83,45,32,21,19,22,23,42,120)

plotting this as a timeseries is very simple:

plot(data,type=”l”)

and then you realise that you want something meaningful on the x-axis and you write the follow

dates<-c(3500,3400,3300,3200,3100,3000,2900,2800,2700,2600,2500,2400,2300,2200,2100)

 perhaps, if you know a bit of R you’ll choose the more elegant nested function

dates<-sort(seq(2100,3500,100),decreasing=TRUE)

In any case you’ll try to plot this as follow:

plot(x=dates,y=data,type=”l”)

and you’ll find out that R ignored the ordering of the vector dates, and it even reversed your time-series.

My practical solution was to use negative values on the plot, and then delete the “-” with gimp or something (yes I should really be ashamed of myself).
Well for the small portion of people who had the same problem and here’s the solution

plot(x=dates,y=data,type=”l”,xlim=c(max(dates),min(dates)))

Basically you can tell to the plot function that the range of values for the x axis is from the greatest value (the oldest date in our case, thus the largest number) to the smallest value. R will simply then read the values of dates in the correct order and plot the TS in the way it should look like.
Easy.

To leave a comment for the author, please follow the link and comment on their blog: EvolvingSpaces.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)