Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace numbers on X axis by dates when using plot in R?

Tags:

plot

r

I would like to present the x lab as date not numbers. if you,for instance , plot:

 f=c(2,1,5,4,8,9,5,2,1,4,7)
 plot(f)

you would get on the x axis numbers range according to how many values we have. How can I set for example my first value as 04/01/2012 and the second value to be 05/01/2012 and so on and then present them on x axis as date not numbers!!

I do not have date in my data but I know the first date.

Thanks in advance

like image 325
hyat Avatar asked Mar 22 '13 16:03

hyat


People also ask

How do I change the X-axis values in R plot?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I change the X-axis labels in R studio?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How do you change X label in R?

Key ggplot2 R functionsp + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.

How do I change the X-axis tick marks in R?

Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks.


1 Answers

You can either label the axis yourself or get R to do it for you by creating a vector of dates for your observations using the "Date" class. Here is an example:

f <- c(2,1,5,4,8,9,5,2,1,4,7)
dates <- seq(as.Date("04/01/2012", format = "%d/%m/%Y"),
             by = "days", length = length(f))

plot(dates, f)

dates ends up being:

> dates
 [1] "2012-01-04" "2012-01-05" "2012-01-06" "2012-01-07" "2012-01-08"
 [6] "2012-01-09" "2012-01-10" "2012-01-11" "2012-01-12" "2012-01-13"
[11] "2012-01-14"

and the plot looks like this:

enter image description here

If you need more control and what the labels exactly as you have them, you need to suppress drawing the x-axis and then add it manually using axis.Date, e.g.

plot(dates, f, xaxt = "n")
axis.Date(side = 1, dates, format = "%d/%m/%Y")

which produces

enter image description here

You may also want to rotate the axis labels there, say by using las = 2.

See ?axis.Date, ?strftime and ?as.Date for further details.

Finer control over the placement of labels using axis.Date

To override the default heuristic for tick placement, specify the locations for the ticks using the at argument. For example, with a longer date sequence of 700 days we might place labels at the start of each month:

set.seed(53)
f <- rnorm(700, 2)
dates <- seq(as.Date("04/01/2012", format = "%d/%m/%Y"),
             by = "days", length = length(f))
head(f)

The plotting is slightly more involved but not much

op <- par(mar = c(7,4,4,2) + 0.1) ## more space for the labels
plot(dates, f, xaxt = "n", ann = FALSE)
labDates <- seq(as.Date("01/01/2012", format = "%d/%m/%Y"), tail(dates, 1),
                by = "months")
axis.Date(side = 1, dates, at = labDates, format = "%b %y", las = 2)
title(ylab = "f") ## draw the axis labels
title(xlab = "dates", line = 5) ## push this one down a bit in larger margin
par(op) ## reset margin

This results in:

enter image description here

You can vary this theme, e.g. label every other month, minor tick for other months

op <- par(mar = c(7,4,4,2) + 0.1) ## more space for the labels
plot(dates, f, xaxt = "n", ann = FALSE)
labDates <- seq(as.Date("01/01/2012", format = "%d/%m/%Y"), tail(dates, 1),
                by = "2 months")
## new dates for minor ticks
minor <- seq(as.Date("01/02/2012", format = "%d/%m/%Y"), tail(dates, 1),
             by = "2 months")
axis.Date(side = 1, dates, at = labDates, format = "%b %y", las = 2)
## add minor ticks with no labels, shorter tick length
axis.Date(side = 1, dates, at = minor, labels = FALSE, tcl = -0.25)
title(ylab = "f") ## draw the axis labels
title(xlab = "dates", line = 5) ## push this one down a bit in larger margin
par(op) ## reset margin

which results in

enter image description here

The point is, if you don't like the defaults, you have full control over where the axis is labelled, simply by creating the vector of dates for the locations of the labels/tick marks you desire.

like image 65
Gavin Simpson Avatar answered Sep 23 '22 23:09

Gavin Simpson