arrows () when x-axis is dates

4 views
Skip to first unread message

Brett Scheffers

unread,
Oct 15, 2012, 11:06:52 PM10/15/12
to tropi...@googlegroups.com
Hello,

Does anyone know how to put arrows on a plot when the x-axis are dates? The x-axis dates start on 26/8/2011 and end 26/9/2011.

For example, I wish I could do this:

plot(dates, d1$bnf1, type="l", frame=FALSE, main="Big", col="blue", ylab="Proportion Weight Increase/Decrease", cex=1.1, cex.axis=1.5, cex.lab=1.5, xlab="",  ylim=c(0.3, 1))

arrows("2011-09-05",0.3,"2011-09-05",0.7, length=0, lty=2)

Thank you!

Brett
--

Brett R Scheffers

 
Department of Biological Sciences

National University of Singapore

Singapore 117543, Singapore
 
Center for Tropical Biology and Climate Change
James Cook University
Townsville, QLD 4811 AUSTRALIA
 
Skype: schefbr0
 
Lab webpage (Australia): http://laurancelab.org/
 
 

Stewart Macdonald

unread,
Oct 16, 2012, 6:51:39 AM10/16/12
to tropi...@googlegroups.com, Brett Scheffers
Hey Brett,

I don't know what the internals of your 'dates' and 'd1' data structures are, but here's some code that might help. If you're storing your date values as a proper date class, R will represent them internally by the number of days since some origin (which might be platform-specific). So to get your arrows in the correct location, you just need to convert your human-readable date into the number of days since that origin. I'm keeping it simple below by just referencing the date objects I've already created.

###############
Lines <- "Date Weight
1/10/2012 69
2/10/2012 71
3/10/2012 79
4/10/2012 80
5/10/2012 70
6/10/2012 53
7/10/2012 69
8/10/2012 75
9/10/2012 71
10/10/2012 70
11/10/2012 82
12/10/2012 64"

reader <- textConnection(Lines)
df <- read.table(reader, header = TRUE)
close(reader)
df$Date <- as.Date(df$Date, "%d/%m/%Y")
plot(df, xaxt = "n", type = "l", frame=FALSE, main="Big", col="blue", ylab="Proportion Weight Increase/Decrease", cex=1.1, cex.axis=1.5, cex.lab=1.5)

axis(1, df$Date, format(df$Date, "%b %d"), cex.axis = .7)

arrows(df$Date[2], df$Weight[2] - 0.2, df$Date[2], df$Weight[2] - 5, code=1, col="red")
arrows(df$Date[8], df$Weight[8] + 0.2, df$Date[8], df$Weight[8] + 5, code=1, col="red")
###############

Let me know if this does and/or doesn't make sense.


Stewart


On 16/10/2012, at 1:06 PM, Brett Scheffers <sche...@gmail.com>
wrote:
> --
> An R group for questions, tips and tricks relevant to spatial ecology and climate change.
> All R questions welcome.
> ---
> You received this message because you are subscribed to the Google Groups "Tropical R" group.
> To post to this group, send an email to tropi...@googlegroups.com.
> To unsubscribe from this group, send email to tropical-r+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Reply all
Reply to author
Forward
0 new messages