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.
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.
Called without arguments xlim returns the x-axis limits of the current plot.
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")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With