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
|
df <- read.csv('timeline_mdh.csv')
df <- df[with(df, order(date)), ]
status_levels <- c("Retroceso", "Normal", "Avance")
status_colors <- c("#008000","#0070C0","#C00000")
# Para que los distintos hitos no se superpongan expandí las opciones posibles de ubicación en el eje y
positions <- c(0.25, -0.25, 0.5, -0.5, 0.75, -0.75, 1.0, -1.0, 1.25, -1.25, 1.5, -1.5)
directions <- c(1, -1)
line_pos <- data.frame(
"date"=unique(df$date),
"position"=rep(positions, length.out=length(unique(df$date))),
"direction"=rep(directions, length.out=length(unique(df$date)))
)
df <- merge(x=df, y=line_pos, by="date", all = TRUE)
df <- df[with(df, order(date)), ]
text_offset <- 0.05
# Para meses, mejor hacerlo y scarlo luego (así queda el template para hacer líneas de tiempo más cortas)
df$month_count <- ave(df$date==df$date, df$date, FUN=cumsum)
df$text_position <- (df$month_count * text_offset * df$direction) + df$position
df <- df %>%
mutate(date = as.Date(date, "%Y-%m-%d"))
month_date_range <- seq(min(df$date) - months(month_buffer), max(df$date) + months(month_buffer), by='month')
month_format <- format(month_date_range, '%m')
month_df <- data.frame(month_date_range, month_format)
year_date_range <- seq(min(df$date) - months(month_buffer), max(df$date) + months(month_buffer), by='year')
year_date_range <- as.Date(
intersect(
ceiling_date(year_date_range, unit="year"),
floor_date(year_date_range, unit="year")
), origin = "1970-01-01"
)
# Años de a dos números mejor que cuatro
year_format <- format(year_date_range, '%y')
year_df <- data.frame(year_date_range, year_format)
timeline_plot<-ggplot(df,aes(x=date,y=0, col=status, label=milestone))
timeline_plot<-timeline_plot+labs(col="Milestones")
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE)
# Los meses se plotean con esto --> pero me lo salteo
timeline_plot<-timeline_plot+geom_text(data=month_df, aes(x=month_date_range,y=-0.1,label=month_format),size=2,vjust=0.5, color='black', angle=0)
timeline_plot<-timeline_plot+theme_classic()
timeline_plot<-timeline_plot+geom_hline(yintercept=0, color = "black", size=0.3, family = "Jost* Book")
timeline_plot<-timeline_plot+geom_segment(data=df[df$month_count == 1,], aes(y=position,yend=0,xend=date), color='black', family = "Jost* Book", size=0.2)
timeline_plot<-timeline_plot+geom_point(aes(y=0), 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<-timeline_plot+geom_text(data=year_df, aes(x=year_date_range,y=-0.1,label=year_format, fontface="bold", family = "Jost* Book"),size=3, color='black')
timeline_plot<-timeline_plot+geom_text(aes(y=text_position,label=milestone),size=4.5, family = "Jost* Book") + theme(legend.position = "none")
print(timeline_plot)
|