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
228 views
in Technique[技术] by (71.8m points)

r - Time series straight line plotting

I'm quite new to R and I have this task I need to complete. I would like top produce a plot like the following.

enter image description here

I did it with excel but I don't like the appearance, I think I can get a better plot using ggplot2.

The sample data for this plot:

date,A,B,C,D,E,F,G,H
1/1/1965,1,,,4,,6,7,
1/2/1965,1,,,4,,6,7,
1/3/1965,1,,,4,,6,7,
1/4/1965,1,,,4,,6,7,
1/5/1965,1,,,4,,6,7,
1/6/1965,1,,,4,,6,7,
1/7/1965,1,,,4,,6,7,
1/8/1965,1,,,4,,6,7,
1/9/1965,1,,,4,,6,7,
1/10/1965,1,,,4,,6,7,
1/11/1965,1,,,4,,6,7,
1/12/1965,1,,,4,,6,7,
1/13/1965,1,,,4,,6,7,
1/14/1965,1,,,4,,6,7,
1/15/1965,1,,,4,,6,7,
1/16/1965,1,2,,4,,6,7,
1/17/1965,1,2,,4,,6,7,
1/18/1965,1,2,,4,,6,7,
1/19/1965,1,2,,4,,6,7,
1/20/1965,1,2,,4,,6,7,
1/21/1965,1,2,,4,,6,7,8
1/22/1965,1,2,,4,,6,7,8
1/23/1965,1,2,,4,,6,7,8
1/24/1965,1,2,,4,,6,7,8
1/25/1965,1,2,,4,,6,7,8
1/26/1965,1,2,,4,,6,7,8
1/27/1965,1,2,,4,,6,7,8
1/28/1965,1,2,,4,,6,7,8
1/29/1965,1,2,,4,,6,7,8
1/30/1965,1,2,,4,,6,7,8
1/31/1965,1,2,,4,,6,7,8
2/1/1965,1,2,,4,,6,7,8
2/2/1965,1,2,,4,,6,7,8
2/3/1965,1,2,,4,,6,7,8
2/4/1965,1,2,,4,,6,7,8
2/5/1965,1,2,,4,,6,7,8
2/6/1965,1,2,,4,,6,7,8
2/7/1965,1,2,,4,,6,7,8
2/8/1965,1,2,,4,,6,7,8
2/9/1965,1,2,,4,,6,7,8
2/10/1965,1,2,,4,,6,7,8
2/11/1965,1,2,,4,,6,7,8
2/12/1965,1,2,,4,,6,7,8
2/13/1965,1,2,,4,,6,7,8
2/14/1965,1,2,,4,,6,7,8
2/15/1965,1,2,,4,,6,7,8
2/16/1965,1,2,,4,,6,7,8
2/17/1965,1,2,,4,,6,7,8
2/18/1965,1,2,,4,,6,7,8
2/19/1965,1,2,,4,,6,7,8
2/20/1965,1,2,,4,,6,7,8
2/21/1965,1,2,,4,,6,7,8
2/22/1965,1,2,,4,,6,7,8
2/23/1965,1,2,,4,,6,7,8
2/24/1965,1,2,,4,,6,7,8
2/25/1965,1,2,,4,,6,7,8
2/26/1965,1,2,,4,,6,7,8
2/27/1965,1,2,,4,,6,7,8
2/28/1965,1,2,,4,,6,7,8
3/1/1965,1,2,,4,,6,7,8
3/2/1965,1,2,,4,,6,7,8
3/3/1965,1,2,,4,,6,7,8
3/4/1965,1,2,,4,,6,7,8
3/5/1965,1,2,,4,,6,7,8
3/6/1965,1,2,,4,,6,7,8
3/7/1965,1,2,,4,,6,7,8
3/8/1965,1,2,,4,,6,7,8
3/9/1965,1,2,,4,,6,7,8
3/10/1965,1,2,,4,,6,7,8
3/11/1965,1,2,,4,,6,7,8
3/12/1965,1,2,,4,,6,7,8
3/13/1965,1,2,,4,,6,7,8
3/14/1965,1,2,,4,,6,7,8
3/15/1965,1,2,,4,,6,7,8
3/16/1965,1,2,,4,,6,7,8
3/17/1965,1,2,,4,,6,7,8
3/18/1965,1,2,,4,,6,7,8
3/19/1965,1,2,,4,,6,7,8
3/20/1965,1,2,,4,,6,7,8
3/21/1965,1,2,,4,,6,7,8
3/22/1965,1,2,,4,,6,7,8
3/23/1965,1,2,,4,,6,7,8
3/24/1965,1,,,4,,6,7,8
3/25/1965,1,,,4,,6,,8
3/26/1965,1,,,4,,6,,8
3/27/1965,1,,,4,,6,,8
3/28/1965,1,,,4,,6,,8
3/29/1965,1,,,4,,6,,8
3/30/1965,1,,,4,,6,,8
3/31/1965,1,,,4,,6,,8
4/1/1965,1,,,4,,6,,8
4/2/1965,1,,,4,,6,,8
4/3/1965,1,,,4,,6,,8
4/4/1965,1,,,4,,6,,8
4/5/1965,1,,,4,,6,,8
4/6/1965,1,,,4,,6,,8
4/7/1965,1,,,4,,6,,8
4/8/1965,1,,,4,,6,,8
4/9/1965,1,,,4,,6,,8
4/10/1965,1,,,4,,6,,8
4/11/1965,1,,,4,,6,,8
4/12/1965,1,,,4,,6,,8
4/13/1965,1,,,4,,6,,8
4/14/1965,1,,,4,,6,,8
4/15/1965,1,,,4,,6,,8
4/16/1965,1,,,4,,6,,8
4/17/1965,1,,,4,,6,,8
4/18/1965,1,,,4,,6,,8
4/19/1965,1,,,4,,6,,8
4/20/1965,1,,,4,,6,,8
4/21/1965,1,,,4,,6,,8
4/22/1965,1,,,4,,6,,8
4/23/1965,1,,,4,,6,,8
4/24/1965,1,,,4,,6,,8
4/25/1965,1,,,4,,6,,8
4/26/1965,1,,,4,,6,,8
4/27/1965,1,,,4,,6,,8
4/28/1965,1,,,4,,6,,8
4/29/1965,1,,,4,,6,,8
4/30/1965,1,,,4,,,,8
5/1/1965,1,,,4,,6,,8
5/2/1965,1,2,,4,,6,,8
5/3/1965,1,2,,4,5,6,,8
5/4/1965,1,2,,4,5,6,,8
5/5/1965,1,2,,4,5,6,,8
5/6/1965,1,2,,4,5,6,,8
5/7/1965,1,2,,4,5,6,,8
5/8/1965,1,2,,4,5,6,,8
5/9/1965,1,2,,4,5,6,,8
5/10/1965,1,2,,4,5,6,,8
5/11/1965,1,2,,4,5,6,,8
5/12/1965,1,2,,4,5,6,,8
5/13/1965,1,2,,4,5,6,,8
5/14/1965,1,2,,4,5,6,,8
5/15/1965,1,2,,4,5,6,,8
5/16/1965,1,2,,4,5,6,,8
5/17/1965,1,2,,4,5,6,,8
5/18/1965,1,2,,4,5,6,,8
5/19/1965,1,2,,4,5,6,,8
5/20/1965,1,2,,4,5,6,,8
5/21/1965,1,2,,4,5,6,,8
5/22/1965,1,2,,4,5,6,,8
5/23/1965,1,2,,4,5,6,,8
5/24/1965,1,2,,4,5,6,,8
5/25/1965,1,2,,4,5,6,,8
5/26/1965,1,2,,4,5,6,,8
5/27/1965,1,2,,4,5,6,,8
5/28/1965,1,2,,4,5,6,,8
5/29/1965,1,2,,4,5,6,,8
5/30/1965,1,2,,4,5,6,,8
5/31/1965,1,2,,4,5,6,,8
6/1/1965,1,2,,4,5,6,,8
6/2/1965,1,2,,4,5,6,,8
6/3/1965,1,2,,4,5,6,,8
6/4/1965,1,2,,4,5,6,,8
6/5/1965,1,2,,4,5,6,,8
6/6/1965,1,2,,4,5,6,,8
6/7/1965,1,2,,4,5,6,,8
6/8/1965,1,2,,4,5,6,,8
6/9/1965,1,2,,4,5,6,,8
6/10/1965,1,2,,4,5,6,,8
6/11/1965,1,2,,4,5,6,,8
6/12/1965,1,2,,4,5,6,,8
6/13/1965,1,2,,4,5,6,,8
6/14/1965,1,2,,4,5,6,,8
6/15/1965,1,2,,4,5,6,,8
6/16/1965,1,2,,4,5,6,,8
6/17/1965,1,2,,4,5,6,,8
6/18/1965,1,2,,4,5,6,,8
6/19/1965,1,2,,4,5,6,,8
6/20/1965,1,2,,4,5,6,,8
6/21/1965,1,2,,4,5,6,,8
6/22/1965,1,2,,4,5,6,,8
6/23/1965,1,2,,4,5,6,,8
6/24/1965,1,2,,4,5,6,,8
6/25/1965,1,2,,4,5,6,,8
6/26/1965,1,2,,4,5,6,,8
6/27/1965,1,2,,4,5,6,,8
6/28/1965,1,2,,4,5,6,,8
6/29/1965,1,2,,4,5,6,,8
6/30/1965,1,2,,4,5,6,,8
7/1/1965,1,2,,4,5,6,,8
7/2/1965,1,2,,4,5,6,,8
7/3/1965,1,2,,4,5,6,,8
7/4/1965,1,2,,4,5,6,,8
7/5/1965,1,2,,4,5,6,,8
7/6/1965,1,2,,4,5,6,,8
7/7/1965,1,2,,4,5,6,,8
7/8/1965,1,2,,4,5,6,,8
7/9/1965,1,2,,4,5,6,,8
7/10/1965,,2,,4,5,6,,8
7/11/1965,,2,,4,5,6,,8
7/12/1965,,2,,4,5,6,,8
7/13/1965,,2,,4,5,6,,8
7/14/1965,,2,,4,5,6,,8
7/15/1965,,2,,4,5,6,,8
7/16/1965,,2,,4,5,6,,8
7/17/1965,,2,,4,5,6,,8
7/18/1965,,2,,4,5,6,,8
7/19/1965,,2,,4,5,6,,8
7/20/1965,,2,,4,5,6,,8
7/21/1965,,2,,4,5,6,,8
7/22/1965,,2,,4,5,6,,8
7/23/1965,,2,,4,5,6,,8
7/24/1965,,2,,4,5,6,,8
7/25/1965,,2,,4,5,6,,8
7/26/1965,,2,,4,5,6,,8
7/27/1965,,2,,4,5,6,,8
7/28/1965,,2,,4,5,6,,8
7/29/1965,,2,,4,5,6,,8
7/30/1965,,2,,4,5,6,,8
7/31/1965,,2,,4,5,6,,8
8/1/1965,,2,,4,5,6,,8
8/2/1965,,2,,4,5,6,,8
8/3/1965,,2,,4,5,6,,8
8/4/1965,,2,,4,5,6,,8
8/5/1965,,2,,4,5,6,,8
8/6/1965,,2,,4,5,6,,8
8/7/1965,,2,,4,5,6,,8
8/8/1965,,2,,4,5,6,,8
8/9/1965,,2,,4,5,6,,8
8/10/1965,,2,,4,5,6,,8
8/11/1965,,2,,4,5,6,,8
8/12/1965,,2,,4,5,6,,8
8/13/1965,,2,,4,5,6,,8
8/14/1965,,2,,4,5,6,,8
8/15/1965,,2,,4,5,6,,8
8/16/1965,,2,,4,5,6,,8
8/17/1965,,2,,4,5,6,,8
8/18/1965,,2,,4,5,6,,8
8/19/1965,,2,,4,5,6,,8
8/20/1965,,2,,4,5,6,,8
8/21/1965,,2,,4,5,6,,8
8/22/1965,,2,,4,5,6,,8
8/23/1965,,2,,4,5,6,,8
8/24/1965,,2,,4,5,6,,8
8/25/1965,,2,,4,5,6,,8
8/26/1965,,2,,4,5,6,,8
8/27/1965,,2,,4,5,6,,8
8/28/1965,,2,,4,5,6,,8
8/29/1965,,2,,4,5,6,,8
8/30/1965,,2,,4,5,6,,8
8/31/1965,,2,,4,5,6,,8
9/1/1965,,2,,4,5,6,,8
9/2/1965,,2,,4,5,6,,8
9/3/1965,,2,,4,5,6,,8
9/4/1965,,2,,4,5,6,,8
9/5/1965,1,2,,,5,,7,8
9/6/1965,1,2,,,5,,7,8
9/7/1965,1,2,,,5,,7,8
9/8/1965,1,2,,,5,,7,8
9/9/1965,1,2,,,5,,7,8
9/10/1965,1,2,,,5,,7,8
9/11/1965,1,2,,,5,,7,8
9/12/1965,1,2,,,5,,7,8
9/13/1965,1,2,,,5,,7,8
9/14/1965,1,2,,,5,,7,8
9/15/1965,1,2,,,5,,7,8
9/16/1965,1,2,,,5,,7,8
9/17/1965,1,2,,,5,,7,8
9/18/1965,1,2,,,5,,7,8
9/19/1965,1,2,,,5,,7,8
9/20/1965,1,2,,,5,,7,8
9/21/1965,1,2,,,5,,7,8
9/22/1965,1,2,,,5,,7,8
9/23/1965,1,2,,,5,,7,8
9/24/1965,1,2,,,5,,7,8
9/25/1965,1,2,,,5,,7,8
9/26/1965,1,2,,,5,,7,8
9/27/1965,1,2,,,5,,7,8
9/28/1965,1,2,,,5,,7,8
9/29/1965,1,2,,,5,,7,8
9/30/1965,1,2,,,5,,7,8
10/1/1965,1,2,,,5,,7,8
10/2/1965,1,2,,,5,,7,8
10/3/1965,1,2,,,5,,7,8
10/4/1965,1,2,,,5,,7,8
10/5/1965,1,2,,,5,,7,8
10/6/1965,1,2,,,5,,7,8
10/7/1965,1,2,,,5,,7,8
10/8/1965,1,2,,,5,,7,8
10/9/1965,1,2,,,5,,7,8
10/10/1965,1,2,,,5,,7,8
10/11/1965,1,2,,,5,,7,8
10/12/1965,1,2,,,5,,7,8
10/13/1965,1,2,,,5,,7,8
10/14/1965,1,2,,,5,,7,8
10/15/1965,1,2,,,5,,7,8
10/16/1965,1,2,,,5,,7,8
10/17/1965,1,2,,,5,,7,8
10/18/1965,1,2,,,5,,7,8
10/19/1965,1,2,,,5,,7,8
10/20/1965,1,2,,,5,,7,8
10/21/1965,1,2,,,5,,7,8
10/22/1965,1,2,,,5,,7,8
10/23/1965,1,2,,,5,,7,8
10/24/1965,1,2,,,5,,7,8
10/25/1965,1,2,,,5,,7,8
10/26/1965,1,2,,,5,,7,8
10/27/1965,1,2,,,5,,7,8
10/28/1965,1,2,,,5,,7,8
10/29/1965,1,2,,,5,,7,8
10/30/1965,1,2,,,5,,7,8
10/31/1965,1,2,,,5,,7,8
11/1/1965,1,2,,,5,,7,8
11/2/1965,1,2,,,5,,7,8
11/3/1965,1,2,,,5,,7,8
11/4/1965,1,2,,,5,,7,8
11/5/1965,1,2,,,5,,7,8
11/6/1965,1,2,,,5,,7,8
11/7/1965,1,2,,,5,,7,8
11/8/1965,1,2,,,5,,7,8
11/9/1965,1,2,,,5,,7,8
11/10/1965,1,2,,,5,,7,8
11/11/1965,1,2,,,5,,7,8
11/12/1965,1,2,,,5,,7,8
11/13/1965,1,2,,,5,,7,8
11/14/1965,1,2,,,5,,7,8
11/15/1965,1,2,,,5,,7,8
11/16/1965,1,2,,,5,,7,8
11/17/1965,1,2,,,5,,7,8
11/18/1965,1,2,,,5,,7,8
11/19/1965,1,2,,,5,,7,8
11/20/1965,1,2,,,5,,7,8
11/21/1965,1,2,,,5,,7,8
11/22/1965,1,2,,,5,,7,8
11/23/1965,1,2,,,5,,7,8
11/24/1965,1,2,3,,5,6,7,8
11/25/1965,1,2,3,,5,6,7,8
11/26/1965,1,2,3,,5,6,7,8
11/27/1965,1,2,3,,5,6,7,8
11/28/1965,1,2,3,,5,6,7,8
11/29/1965,1,2,3,,5,6,7,8
11/30/1965,1,2,3,,5,6,7,8
12/1/1965,1,2,3,,5,6,7,8
12/2/1965,1,2,3,,5,6,7,8
12/3/1965,1,2,3,,5,6,7,8
12/4/1965,1,2,3,,5,6,7,8
12/5/1965,1,2,3,,5,6,7,8
12/6/1965,1,2,3,,5,6,7,8
12/7/1965,1,2,3,,5,6,7,8
12/8/1965,1,2,3,,5,6,7,8
12/9/1965,1,2,3,,5,6,7,8
12/10/1965,1,2,3,,5,6,7,8
12/11/1965,1,2,3,,5,6,7,8
12/12/1965,1,2,3,,5,6,7,8
12/13/1965,1,2,3,,5,6,7,8
12/14/1965,1,2,3,,5,6,7,8
12/15/1965,1,2,3,,5,6,7,8
12/16/1965,1,2,3,,5,6,7,8
12/17/1965,1,2,3,,5,6,7,8
12/18/1965,1,2,3,,5,6,7,8
12/19/1965,1,2,3,,5,6,7,8
12/20/1965,1,2,3,,5,6,7,8
12/21/1965,1,2,3,,5,6,7,8
12/22/1965,1,2,3,,,6,7,8
12/23/1965,1,2,3,,,6,7,8
12/24/1965,1,2,3,,,6,7,8
12/25/1965,1,2,3,,,6,7,8
12/26/1965,1,2,3,,,6,7,8
12/27/1965,1,2,3,,,6,7,8
12/28/1965,1,2,3,,,6,7,8
12/29/1965,1,2,3,,,6,7,8
12/30/1965,1,2,3,,,6,7,8
12/31/1965,1,2,3,,,6,7,8

My Data frame can be found in the following link

https://drive.google.com/file/d/1IQl_KKNONI7WiBloSmS_ZepVlakRBpUt/view?usp=sharing

question from:https://stackoverflow.com/questions/65843883/time-series-straight-line-plotting

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

1 Reply

0 votes
by (71.8m points)

Try reshaping and using geom_line():

library(dplyr)
library(tidyr)
library(ggplot2)
#Load data
df <- read.csv('SampleData.csv',stringsAsFactors = F)
#Code
df %>%
  mutate(date=as.Date(date,'%m/%d/%Y')) %>%
  pivot_longer(-date) %>%
  ggplot(aes(x=date,y=value,group=name,color=name))+
  geom_line()+
  scale_x_date(date_labels = '%Y-%m-%d',breaks = '5 year')+
  scale_y_continuous(breaks = seq(0,10,by=1))

Output:

enter image description here


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

...