AmirHossein Ahrari
AmirHossein Ahrari

Reputation: 115

How to Show Dates on x Axis ggplot in R Programming Language?

I have this data file:

       Time      Area  Height
1 2/26/2000 484226449 1560.46
2  3/5/2000 475053975 1560.42
3 3/13/2000 466963590 1560.39
4 3/21/2000 441697246 1560.29
5 3/29/2000 428258729 1560.25
6  4/6/2000 408551641 1560.16

The ggplot shows chart but date ranges doesn't show on xAxis:

ggplot(t2, aes(x = Time, y = Area, group = 1)) + geom_point() + geom_line()

enter image description here

Upvotes: 0

Views: 52

Answers (1)

stefan
stefan

Reputation: 125537

Maybe this is what you are looking for. First convert your Timevariable to date format. Second. Depending on your desired format you can set the number of breaks and the format for the labels via scale_x_date:

library(ggplot2)

t2 <- read.table(text = "Time      Area  Height
1 2/26/2000 484226449 1560.46
2  3/5/2000 475053975 1560.42
3 3/13/2000 466963590 1560.39
4 3/21/2000 441697246 1560.29
5 3/29/2000 428258729 1560.25
6  4/6/2000 408551641 1560.16", header = TRUE)

t2$Time <- as.Date(t2$Time, "%m/%d/%Y")
ggplot(t2, aes(x = Time, y = Area)) + 
  geom_point() + 
  geom_line() +
  scale_x_date(date_labels = "%d %b %Y")

Upvotes: 1

Related Questions