Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

ggplot2 - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...