Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting xlim with dates in R

Tags:

plot

r

as.date

I need to set the x-axis limit of a plot in R, but my values are dates (%m/%d/%Y format). How would I go about changing this? I am trying to plot trophic position vs. collection date. All of my collection dates are in date format (%m/%d/%Y)

This is the code I have tried:

plot(Trophic_Position~Collection_Date, data=BO,main="Burr Oak", col="red",xlab="Collection date",ylab="Trophic Position", xlim=c("6/09/2014","8/30/2014"), ylim=c(2,5))

I have just started to learn R, so I know that there must be a code that goes along with the xlim command, but I haven't been able to find out what code applies to my situation.

like image 683
M. Bittner Avatar asked Sep 02 '16 17:09

M. Bittner


People also ask

How do you XLIM a date?

You need to provide the xlim values in Date format, rather than as character strings: xlim=as. Date(c("2014-06-09", "2014-08-30")) . @eipi10 you should post this as an answer so it can be accepted as the right answer.

What does the function XLIM () return?

Called without arguments xlim returns the x-axis limits of the current plot.


1 Answers

Try using ggplot2. Your figure with required restricted X values - date can be obtained from,

library(ggplot2)

g1 = ggplot(ENTER_YOUR_DATAFRAME_NAME, aes(x=Collection_Date, y=B0))+geom_line()
g1+ scale_x_date(limits = as.Date(c("2014-06-09","2014-08-30")))
like image 79
Ebby Avatar answered Oct 12 '22 03:10

Ebby