1

I was trying to plot two columns of my data.frame (let's call them data$Date, data$x for convenience).

When plotting data$x I want to limit myself to data from after 2014-01-01. What's the proper way of doing that?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Dinosaur
  • 645
  • 4
  • 10
  • 14
  • For convenience, could you make your example reproducible? –  Apr 14 '15 at 08:25
  • 1
    Reproducible [like so](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – ROLO Apr 14 '15 at 08:30

2 Answers2

1

You can first put your Date column as Date with strptime and then subset your data.frame to keep only dates post 2014-01-01:

data$Date <- strptime(data$Date, format="%y%y-%m-%d")
plot(data$Date[data$Date > strptime("2014-01-01", format="%y%y-%m-%d")], data$x[data$Date > strptime("2014-01-01", format="%y%y-%m-%d")], las=1, pch=19, xlab="Date post 01/01/2014", ylab="")

Example

 data <- data.frame(Date=c("2013-02-03","2013-12-13","2014-02-02","2014-05-05"), x=1:4, stringsAsFactors=F)

enter image description here

EDIT
If you need to plot more than one "x", you can first use plot and then points. You can adjust the x axis with axis.POSIXct:

data <- data.frame(Date=c("2013-02-03", "2013-12-13", "2014-02-02", "2014-05-05", "2015-04-14"), x1=rep(1, 5), x2=rep(2, 5), stringsAsFactors=F)
data$Date <- strptime(data$Date, format="%y%y-%m-%d")
plot(data$Date[data$Date > strptime("2014-01-01", format="%y%y-%m-%d")], data$x1[data$Date > strptime("2014-01-01", format="%y%y-%m-%d")], las=1, pch=19, col="red", xlab="Date post 01/01/2014", ylab="", xaxt="n", ylim=c(0,3))
points(data$Date[data$Date > strptime("2014-01-01", format="%y%y-%m-%d")], data$x2[data$Date > strptime("2014-01-01", format="%y%y-%m-%d")], pch=19, col="blue")
axis.POSIXct(1, at=seq(strptime("2014-02-01",format="%y%y-%m-%d"),strptime("2015-04-01",format="%y%y-%m-%d"), by = "month"), format = "%m/%y")

enter image description here

Cath
  • 23,906
  • 5
  • 52
  • 86
  • Thank you, CathG. How can I add a key to the plot (in case I have more than one parameter)? – Dinosaur Apr 14 '15 at 09:22
  • @Dinosaur, you're welcome. Sorry but I don't understand what you mean by "add a key to the plot", what parameter you have that can be more than one, can you give me an example to help me better understand what you mean ? – Cath Apr 14 '15 at 09:26
  • Sorry I wasn't clear enough. For example, if I want to plot data$x1 in red and data$x2 in blue against data$Date on the same graph, how can I indicate which graph is which? Also, how could I adjust the "nodes" on the x-axis? At the moment, I only have two on the x-axis, "2014" and "2015". Thanks again for your help! – Dinosaur Apr 14 '15 at 09:32
  • you can first plot `data$x1` with `col="red"` and then use `points` to plot `data$x2` with `col="blue"`. As for `x axis` you can plot it with `axis`. I'll edit my answer. – Cath Apr 14 '15 at 09:35
0

If your data frame stores the date as a standard date type, you can also set the xlim option in plot using the date type, e.g.:

xlim<-c(as.Date("20140101","%Y%m%d"),as.Date("20180801","%Y%m%d"))
plot(data$Date,data$x,xlim=xlim)
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86