-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timelines.Rmd
198 lines (164 loc) · 8.45 KB
/
Timelines.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
title: "Timelines"
author: "Francisca Ortiz"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 3
---
## I. Timeline to represent stages (Gantt)
A good example of this, it is the representation of the stages in a research. In this case, it is represented four big stages of my PhD thesis research: Theoretical frame, Fieldwork, Analysis and Writing. Something benefitial of using this package is it allows a simple and comfortably visual with a simple code.
### Firts, load the package "vistime".
```{r message=FALSE, warning=FALSE}
#install.packages("vistime")
library("vistime")
```
### Second, construct the data frame.
```{r message=FALSE, warning=FALSE}
timeline_data <- data.frame(event = c("Theoretical frame", "Fieldwork", "Analysis", "Writing"),
start = c("2018-09-01", "2019-09-01", "2019-12-01", "2020-01-02"),
end = c("2019-09-01", "2020-01-15", "2020-10-01", "2021-09-01"),
group = "Stages of
the research")
```
### Third, select the better option for your research: static or interactive visualizations.
A static option of the visualization:
````{r message=FALSE, warning=FALSE}
gg_vistime(timeline_data)
```
An interactive option of the same visualization:
````{r message=FALSE, warning=FALSE}
hc_vistime(timeline_data)
```
There is another interactive option:
````{r message=FALSE, warning=FALSE}
vistime(timeline_data)
```
### Arguments of each function.
- **vistime**(data, col.event = "event", col.start = "start", col.end = "end", col.group = "group", col.color = "color", col.fontcolor = "fontcolor", col.tooltip = "tooltip", optimize_y = TRUE, linewidth = NULL, title = NULL, show_labels = TRUE, background_lines = NULL)
- **hc_vistime**(data, col.event = "event", col.start = "start", col.end = "end", col.group = "group", col.color = "color", optimize_y = TRUE, title = NULL, show_labels = TRUE)
- **gg_vistime**(data, col.event = "event", col.start = "start", col.end = "end", col.group = "group", col.color = "color", col.fontcolor = "fontcolor", optimize_y = TRUE, linewidth = NULL, title = NULL, show_labels = TRUE, background_lines = NULL)
## II. Timeline to represent many specific events.
**For this timeline, I used the ones done in the next webpage: https://www.themillerlab.io/post/timelines_in_r/**
I used to work with many life history at the same time. And maybe you would be able to manage well with less than ten cases, however, when you got more than that it starts to get more problematic. Therefore, a useful way to work with all those cases is using one timeline for each one. It is important to highlith that this is a complementary tool for the researcher, however, it is not a repleasement of the deep information of a life history.
```{r message=FALSE, warning=FALSE}
library(scales)
library(lubridate)
library(ggplot2)
library(tidyverse)
library(knitr)
library(timevis)
```
### First, let's create a database.
In this particular case, I will used as an example the academic event in which I had participated in one year.
```{r message=FALSE, warning=FALSE}
CaseFrancisca <- data.frame(
Year = c(rep(c(2019), times =6), rep(c(2020), times =7)),
Months = c(4,6,6,7,10,10,5,7,7,7,7,7,7),
Days = c(24,7,13,9,1,3,20,2,7,13,14,15,23),
Milestones = c("BSA Annual Conference", "PGR Sociology Conference",
"MICRA Conference", "BSG Conference", "MICRA", "PhD Colloquium", "MICRA Conference", "BSG Conference", "Jornada de Redes 2020", "Panel Hispanic SUNBELT","SUNBELT", "SUNBELT", "ELSOC"),
Event_type= c("Presenter", "Presenter", "Presenter", "Presenter", "Organizer", "Organizer", "Organizer", "Presenter","Organizer","Organizer","Presenter", "Presenter", "Presenter"))
```
Add one column of the date.
```{r message=FALSE, warning=FALSE}
CaseFrancisca$date <- with(CaseFrancisca, ymd(sprintf('%04d%02d%02d', CaseFrancisca$Year, CaseFrancisca$Months, CaseFrancisca$Days)))
CaseFrancisca <- CaseFrancisca[with(CaseFrancisca, order(date)), ]
kable(head(CaseFrancisca))
```
Add one colours per type of event. In this case I used my role at the academic event, as a presenter or organizer.
```{r message=FALSE, warning=FALSE}
Event_type_levels <- c("Presenter", "Organizer")
Event_type_colors <- c("#C00000", "#FFC000")
```
### Define the vectors of the events, and heigths of the milestones.
```{r message=FALSE, warning=FALSE}
CaseFrancisca$Event_type <- factor(CaseFrancisca$Event_type, levels= Event_type_levels, ordered=TRUE)
positions <- c(0.5, -0.5, 1.0, -1.0, 1.25, -1.25, 1.5, -1.5)
directions <- c(1, -1)
```
Add positions and directions:
```{r message=FALSE, warning=FALSE}
line_pos <- data.frame(
"date"=unique(CaseFrancisca$date),
"position"=rep(positions, length.out=length(unique(CaseFrancisca$date))),
"direction"=rep(directions, length.out=length(unique(CaseFrancisca$date))))
CaseFrancisca <- merge(x=CaseFrancisca, y=line_pos, by="date", all = TRUE)
```
Create a one month "buffer" at the start and end of the timeline
```{r message=FALSE, warning=FALSE}
month_buffer <- 1
month_date_range <- seq(min(CaseFrancisca$date) - months(month_buffer), max(CaseFrancisca$date) + months(month_buffer), by='month')
```
### Eddit the format of months, in three letter abbreviations and range.
```{r message=FALSE, warning=FALSE}
month_format <- format(month_date_range, '%b')
month_df <- data.frame(month_date_range, month_format)
year_date_range <- seq(min(CaseFrancisca$date) - months(month_buffer), max(CaseFrancisca$date) + months(month_buffer), by='year')
```
Add format of year label
```{r message=FALSE, warning=FALSE}
year_date_range <- as.Date(
intersect(
ceiling_date(year_date_range, unit="year"),
floor_date(year_date_range, unit="year")),
origin = "1970-01-01")
year_format <- format(year_date_range, '%Y')
year_df <- data.frame(year_date_range, year_format)
```
### Now for doing the plot, there are seven steps.
1. Create timeline coordinates and label milestones.
```{r message=FALSE, warning=FALSE}
timeline_plot<-ggplot(CaseFrancisca,aes(x=date,y= position,
col=Event_type, label=CaseFrancisca$Milestones))
timeline_plot<-timeline_plot+labs(col="Milestones")
```
2. Add colors and order of the milestones.
```{r message=FALSE, warning=FALSE}
timeline_plot<-timeline_plot+scale_color_manual(values=Event_type_colors, labels=Event_type_levels, drop = FALSE)
timeline_plot<-timeline_plot+theme_classic()
timeline_plot<-timeline_plot+geom_hline(yintercept=0, color = "black", size=0.3)
```
3. Plot the vertical lines for the events, and remove X.
```{r message=FALSE, warning=FALSE}
timeline_plot<-timeline_plot+geom_segment(data=CaseFrancisca, aes(y=CaseFrancisca$position,yend=0,xend=CaseFrancisca$date), color='black', size=0.2)
timeline_plot<-timeline_plot+geom_point(aes(y=CaseFrancisca$position), size=3)
timeline_plot<-timeline_plot+theme(axis.line.y=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x =element_blank(),
axis.ticks.x =element_blank(),
axis.line.x =element_blank(),
legend.position = "bottom")
timeline_plot
```
4. Add text in each month and years.
```{r message=FALSE, warning=FALSE}
timeline_plot<-timeline_plot+geom_text(data=month_df, aes(x=month_date_range,y=-0.15,label=month_format),size=3.5,vjust=0.5, color='black', angle=90)
timeline_plot<-timeline_plot+geom_text(data=year_df, aes(x=year_date_range, y=-0.25,label=year_format, fontface="bold"),size=3.5, color='black')
```
5. Add labels for each milestone event.
```{r message=FALSE, warning=FALSE}
text_offset <- 0.2
absolute_value<-(abs(CaseFrancisca$position))
text_position<- absolute_value + text_offset
CaseFrancisca$text_position<- text_position * CaseFrancisca$direction
```
6.Now we can add the labels to the timeline for our milestones.
```{r message=FALSE, warning=FALSE}
timeline_plot<-timeline_plot+geom_text(aes(y=CaseFrancisca$text_position, label=CaseFrancisca$Milestones),size=3.5, vjust=0.6)
```
7. Plot!
Plot as a static timeline
```{r message=FALSE, warning=FALSE}
timeline_plot
```
Plot an interactive timeline
```{r message=FALSE, warning=FALSE}
#install.packages("plotly")
library(plotly)
ggplotly(timeline_plot)
```