Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. dput(ejemplo)
  2. structure(list(Año = c(2013L, 2014L, 2014L, 2014L, 2014L, 2014L,
  3. 2014L, 2014L, 2014L, 2014L), Mes = c(12L, 1L, 5L, 5L, 8L, 9L,
  4. 10L, 10L, 10L, 11L), Evento = c("Aprobación ", "Instrucción",
  5. "Solicitud uno", "Solicitud dos", "Creación de organismo", "Elección de administrador",
  6. "Aprobación dos", "Constitución comité", "Constitución organización",
  7. "Salida del programa"), Fecha = structure(c(16040, 16071, 16191,
  8. 16191, 16283, 16314, 16344, 16344, 16344, 16375), class = "Date"),
  9. Actor = c("Jefe", "Consejo", "Organismo", "Secretaría", "Tribunal",
  10. "Organismo", "Tribunal", "Consejo", "Organismo", "Organismo"
  11. )), class = "data.frame", row.names = c(NA, -10L))
  12.  
  13. Actor_levels <- c("Jefe",
  14. "Consejo", "Organismo", "Secretaría",
  15. "Tribunal")
  16. Actor_colors <- c("#FF69B4","#0070C0", "#00B050", "#FFC000", "#C00000",
  17. ordered=TRUE)
  18.  
  19. ejemplo$Actor <- factor(ejemplo$Actor, levels=Actor_levels)
  20.  
  21. positions <- c(0.5, -0.5, 1.0, -1.0, 1.5, -1.5)
  22. directions <- c(1, -1)
  23.  
  24. line_pos <- data.frame(
  25. "Fecha"=unique(ejemplo$Fecha),
  26. "position"=rep(positions, length.out=length(unique(ejemplo$Fecha))),
  27. "direction"=rep(directions, length.out=length(unique(ejemplo$Fecha)))
  28. )
  29.  
  30. df <- merge(x=ejemplo, y=line_pos, by="Fecha", all = TRUE)
  31. df <- df[with(df, order(Fecha)), ]
  32. df
  33.  
  34. text_offset <- 0.05
  35.  
  36. df$month_count <- ave(df$Mes==df$Mes, df$Mes, FUN=cumsum)
  37. df$text_position <- (df$month_count * text_offset * df$direction) + df$position
  38. df
  39.  
  40. month_buffer <- 2
  41.  
  42. month_date_range <- seq(min(df$Fecha) - months(month_buffer), max(df$Fecha) + months(month_buffer), by='month')
  43. month_format <- format(month_date_range, '%B')
  44. month_df <- data.frame(month_date_range, month_format)
  45.  
  46.  
  47. year_date_range <- seq(min(df$Fecha) - months(month_buffer), max(df$Fecha) + months(month_buffer), by='year')
  48. year_date_range <- as.Date(
  49. intersect(
  50. ceiling_date(year_date_range, unit="year"),
  51. floor_date(year_date_range, unit="year")
  52. ), origin = "1970-01-01"
  53. )
  54. year_format <- format(year_date_range, '%Y')
  55. year_df <- data.frame(year_date_range, year_format)
  56.  
  57. timeline_plot<-ggplot(df,aes(x=Fecha,y=0, col=Actor, label=Evento))
  58. timeline_plot<-timeline_plot+labs(col="")
  59. timeline_plot<-timeline_plot+scale_color_manual(values=Actor_colors, labels=Actor_levels, drop = FALSE)
  60. timeline_plot<-timeline_plot+theme_classic()
  61.  
  62. timeline_plot<-timeline_plot+geom_hline(yintercept=0,
  63. color = "black", size=0.3)
  64.  
  65. timeline_plot<-timeline_plot+geom_segment(data=df[df$month_count == 1,], aes(y=position,yend=0,xend=Fecha), color='black', size=0.2)
  66.  
  67. timeline_plot<-timeline_plot+geom_point(aes(y=0), size=3)
  68.  
  69. timeline_plot<-timeline_plot+theme(axis.line.y=element_blank(),
  70. axis.text.y=element_blank(),
  71. axis.title.x=element_blank(),
  72. axis.title.y=element_blank(),
  73. axis.ticks.y=element_blank(),
  74. axis.text.x =element_blank(),
  75. axis.ticks.x =element_blank(),
  76. axis.line.x =element_blank(),
  77. legend.position = "bottom"
  78. )
  79.  
  80. timeline_plot<-timeline_plot+geom_text(data=month_df, aes(x=month_date_range,y=-0.1,label=month_format),size=2.5,vjust=0.5, color='black', angle=90)
  81. timeline_plot<-timeline_plot+geom_text(data=year_df, aes(x=year_date_range,y=-0.2,label=year_format, fontface="bold"),size=2.5, color='black')
  82. timeline_plot<-timeline_plot+geom_text(aes(y=text_position,label=Evento),size=2.5)
  83. print(timeline_plot)
  84.  
  85. Warning message:
  86. Removed 10 rows containing missing values (geom_point).
  87.  
  88. timeline_plot<-timeline_plot+geom_point(aes(y=0), size=3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement