Dispersion
require(ggplot2)
## Loading required package: ggplot2
require(plotly)
## Loading required package: plotly
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
require(RSocrata)
## Loading required package: RSocrata
require(lubridate)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
token <- "ew2rEMuESuzWPqMkyPfOSGJgE"
casos_ins <- read.socrata("https://www.datos.gov.co/resource/gt2j-8ykr.json", app_token = token)
casos_ins=casos_ins[casos_ins$ciudad_de_ubicaci_n=="Cali",]
casos_ins$edad=as.numeric(casos_ins$edad)
### GRAFICO DE HISTOGRAMAS geom_histogram
p=ggplot(data = casos_ins,aes(x=edad))+geom_histogram()
p=ggplot(casos_ins,aes(x=edad))+geom_histogram()
p
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#transforma el grafico de ggplot en grafico dinamico
ggplotly(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
### facet_grid + FILL (COLOR)
p4=ggplot(casos_ins,aes(x=edad, fill=sexo))+geom_histogram()+facet_grid(~sexo)
p4
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

### GRAFICO DE SERIES DE TIEMPO geom_bar() - tendencias
#-- Funcion CLASS: indica el tipo de datos
#-- Funcion as.Date: convetir a formato fecha
#-- Crea un nuevo campo fecha (casos_ins$fecha_not) en la base casos_ins
class(casos_ins$fecha_de_notificaci_n)
## [1] "character"
casos_ins$fecha_not=as.Date(casos_ins$fecha_de_notificaci_n)
casos_ins$fis=as.Date(casos_ins$fis)
#-- geom_bar() hace conteo por defecto en eje Y de acuerdo al variable que este en X
p8=ggplot(casos_ins,aes(x=fecha_not))+geom_bar()+theme_bw()
p8

p9=ggplot(casos_ins,aes(x=estado))+geom_bar()+theme_bw()
p9

p10=ggplot(casos_ins,aes(x=fis))+geom_bar()+theme_bw()
p10
## Warning: Removed 88 rows containing non-finite values (stat_count).

## GRAFICO DE DISPERSION - geom_point() se usa para analizar 2 variables numericas
#-- class: reviso que las fechas esten en dato tipo "Date"
#-- table: crea un lista de las fechas y la frecuencia de los casos, esto ingreso a una tabla (tabla1)
#-- data.frame: convierte tabla 1 en dataframe, crea dos vectores uno de fechas y otro con las frecuencias,lo reemplzo nuevamente en tabla1
#-- names: cambiar los nombres de las columnas del data.frame, lo reemplzo nuevamente en tabla1
class(casos_ins$fis)
## [1] "Date"
table(casos_ins$fis)
##
## 2020-03-02 2020-03-05 2020-03-07 2020-03-09 2020-03-10 2020-03-11 2020-03-12
## 1 1 1 4 4 2 9
## 2020-03-13 2020-03-14 2020-03-15 2020-03-16 2020-03-17 2020-03-18 2020-03-19
## 6 10 16 16 14 19 20
## 2020-03-20 2020-03-21 2020-03-22 2020-03-23 2020-03-24 2020-03-25 2020-03-26
## 35 19 14 26 25 27 22
## 2020-03-27 2020-03-28 2020-03-29 2020-03-30 2020-03-31 2020-04-01 2020-04-02
## 16 19 10 23 16 23 20
## 2020-04-03 2020-04-04 2020-04-05 2020-04-06 2020-04-07 2020-04-08 2020-04-09
## 17 14 18 20 17 14 27
## 2020-04-10 2020-04-11 2020-04-12 2020-04-13 2020-04-14 2020-04-15 2020-04-16
## 12 17 18 34 22 27 11
## 2020-04-17 2020-04-18 2020-04-19 2020-04-20 2020-04-21 2020-04-22 2020-04-23
## 14 28 23 43 25 32 23
## 2020-04-24 2020-04-25 2020-04-26 2020-04-27 2020-04-28 2020-04-29 2020-04-30
## 27 26 33 33 33 28 31
## 2020-05-01 2020-05-02 2020-05-03 2020-05-04 2020-05-05 2020-05-06 2020-05-07
## 69 52 36 57 54 49 54
## 2020-05-08 2020-05-09 2020-05-10 2020-05-11 2020-05-12 2020-05-13 2020-05-14
## 65 86 80 68 71 94 94
## 2020-05-15 2020-05-16 2020-05-17 2020-05-18 2020-05-19 2020-05-20 2020-05-21
## 96 86 70 122 92 132 91
## 2020-05-22 2020-05-23 2020-05-24 2020-05-25 2020-05-26 2020-05-27 2020-05-28
## 103 98 73 107 106 109 138
## 2020-05-29 2020-05-30 2020-05-31 2020-06-01 2020-06-02 2020-06-03 2020-06-04
## 118 122 99 247 131 132 111
## 2020-06-05 2020-06-06 2020-06-07 2020-06-08 2020-06-09 2020-06-10 2020-06-11
## 243 173 129 218 201 221 234
## 2020-06-12 2020-06-13 2020-06-14 2020-06-15 2020-06-16 2020-06-17 2020-06-18
## 203 162 150 191 233 221 207
## 2020-06-19 2020-06-20 2020-06-21 2020-06-22 2020-06-23 2020-06-24 2020-06-25
## 245 268 215 349 346 316 421
## 2020-06-26 2020-06-27 2020-06-28 2020-06-29 2020-06-30 2020-07-01 2020-07-02
## 419 371 371 418 481 590 498
## 2020-07-03 2020-07-04 2020-07-05 2020-07-06 2020-07-07 2020-07-08 2020-07-09
## 465 439 437 662 550 494 567
## 2020-07-10 2020-07-11 2020-07-12 2020-07-13 2020-07-14 2020-07-15 2020-07-16
## 640 516 487 619 656 567 579
## 2020-07-17 2020-07-18 2020-07-19 2020-07-20 2020-07-21 2020-07-22 2020-07-23
## 567 518 433 554 577 454 487
## 2020-07-24 2020-07-25 2020-07-26 2020-07-27 2020-07-28 2020-07-29 2020-07-30
## 524 540 423 751 486 560 467
## 2020-07-31 2020-08-01 2020-08-02 2020-08-03 2020-08-04 2020-08-05 2020-08-06
## 497 684 536 523 588 441 446
## 2020-08-07 2020-08-08 2020-08-09 2020-08-10 2020-08-11 2020-08-12 2020-08-13
## 393 389 432 477 373 424 374
## 2020-08-14 2020-08-15 2020-08-16 2020-08-17 2020-08-18 2020-08-19 2020-08-20
## 364 344 326 297 339 285 299
## 2020-08-21 2020-08-22 2020-08-23 2020-08-24 2020-08-25 2020-08-26 2020-08-27
## 238 214 176 245 186 174 152
## 2020-08-28 2020-08-29 2020-08-30 2020-08-31 2020-09-01
## 116 63 40 33 4
tabla1=table(casos_ins$fis)
tabla1=data.frame(tabla1)
tabla1
## Var1 Freq
## 1 2020-03-02 1
## 2 2020-03-05 1
## 3 2020-03-07 1
## 4 2020-03-09 4
## 5 2020-03-10 4
## 6 2020-03-11 2
## 7 2020-03-12 9
## 8 2020-03-13 6
## 9 2020-03-14 10
## 10 2020-03-15 16
## 11 2020-03-16 16
## 12 2020-03-17 14
## 13 2020-03-18 19
## 14 2020-03-19 20
## 15 2020-03-20 35
## 16 2020-03-21 19
## 17 2020-03-22 14
## 18 2020-03-23 26
## 19 2020-03-24 25
## 20 2020-03-25 27
## 21 2020-03-26 22
## 22 2020-03-27 16
## 23 2020-03-28 19
## 24 2020-03-29 10
## 25 2020-03-30 23
## 26 2020-03-31 16
## 27 2020-04-01 23
## 28 2020-04-02 20
## 29 2020-04-03 17
## 30 2020-04-04 14
## 31 2020-04-05 18
## 32 2020-04-06 20
## 33 2020-04-07 17
## 34 2020-04-08 14
## 35 2020-04-09 27
## 36 2020-04-10 12
## 37 2020-04-11 17
## 38 2020-04-12 18
## 39 2020-04-13 34
## 40 2020-04-14 22
## 41 2020-04-15 27
## 42 2020-04-16 11
## 43 2020-04-17 14
## 44 2020-04-18 28
## 45 2020-04-19 23
## 46 2020-04-20 43
## 47 2020-04-21 25
## 48 2020-04-22 32
## 49 2020-04-23 23
## 50 2020-04-24 27
## 51 2020-04-25 26
## 52 2020-04-26 33
## 53 2020-04-27 33
## 54 2020-04-28 33
## 55 2020-04-29 28
## 56 2020-04-30 31
## 57 2020-05-01 69
## 58 2020-05-02 52
## 59 2020-05-03 36
## 60 2020-05-04 57
## 61 2020-05-05 54
## 62 2020-05-06 49
## 63 2020-05-07 54
## 64 2020-05-08 65
## 65 2020-05-09 86
## 66 2020-05-10 80
## 67 2020-05-11 68
## 68 2020-05-12 71
## 69 2020-05-13 94
## 70 2020-05-14 94
## 71 2020-05-15 96
## 72 2020-05-16 86
## 73 2020-05-17 70
## 74 2020-05-18 122
## 75 2020-05-19 92
## 76 2020-05-20 132
## 77 2020-05-21 91
## 78 2020-05-22 103
## 79 2020-05-23 98
## 80 2020-05-24 73
## 81 2020-05-25 107
## 82 2020-05-26 106
## 83 2020-05-27 109
## 84 2020-05-28 138
## 85 2020-05-29 118
## 86 2020-05-30 122
## 87 2020-05-31 99
## 88 2020-06-01 247
## 89 2020-06-02 131
## 90 2020-06-03 132
## 91 2020-06-04 111
## 92 2020-06-05 243
## 93 2020-06-06 173
## 94 2020-06-07 129
## 95 2020-06-08 218
## 96 2020-06-09 201
## 97 2020-06-10 221
## 98 2020-06-11 234
## 99 2020-06-12 203
## 100 2020-06-13 162
## 101 2020-06-14 150
## 102 2020-06-15 191
## 103 2020-06-16 233
## 104 2020-06-17 221
## 105 2020-06-18 207
## 106 2020-06-19 245
## 107 2020-06-20 268
## 108 2020-06-21 215
## 109 2020-06-22 349
## 110 2020-06-23 346
## 111 2020-06-24 316
## 112 2020-06-25 421
## 113 2020-06-26 419
## 114 2020-06-27 371
## 115 2020-06-28 371
## 116 2020-06-29 418
## 117 2020-06-30 481
## 118 2020-07-01 590
## 119 2020-07-02 498
## 120 2020-07-03 465
## 121 2020-07-04 439
## 122 2020-07-05 437
## 123 2020-07-06 662
## 124 2020-07-07 550
## 125 2020-07-08 494
## 126 2020-07-09 567
## 127 2020-07-10 640
## 128 2020-07-11 516
## 129 2020-07-12 487
## 130 2020-07-13 619
## 131 2020-07-14 656
## 132 2020-07-15 567
## 133 2020-07-16 579
## 134 2020-07-17 567
## 135 2020-07-18 518
## 136 2020-07-19 433
## 137 2020-07-20 554
## 138 2020-07-21 577
## 139 2020-07-22 454
## 140 2020-07-23 487
## 141 2020-07-24 524
## 142 2020-07-25 540
## 143 2020-07-26 423
## 144 2020-07-27 751
## 145 2020-07-28 486
## 146 2020-07-29 560
## 147 2020-07-30 467
## 148 2020-07-31 497
## 149 2020-08-01 684
## 150 2020-08-02 536
## 151 2020-08-03 523
## 152 2020-08-04 588
## 153 2020-08-05 441
## 154 2020-08-06 446
## 155 2020-08-07 393
## 156 2020-08-08 389
## 157 2020-08-09 432
## 158 2020-08-10 477
## 159 2020-08-11 373
## 160 2020-08-12 424
## 161 2020-08-13 374
## 162 2020-08-14 364
## 163 2020-08-15 344
## 164 2020-08-16 326
## 165 2020-08-17 297
## 166 2020-08-18 339
## 167 2020-08-19 285
## 168 2020-08-20 299
## 169 2020-08-21 238
## 170 2020-08-22 214
## 171 2020-08-23 176
## 172 2020-08-24 245
## 173 2020-08-25 186
## 174 2020-08-26 174
## 175 2020-08-27 152
## 176 2020-08-28 116
## 177 2020-08-29 63
## 178 2020-08-30 40
## 179 2020-08-31 33
## 180 2020-09-01 4
names(tabla1)=c("fecha_sintomas","Casos")
class(tabla1$fecha_sintomas)
## [1] "factor"
#-- ya tengo la tabla con los dos campos fecha_sintomas","Casos" pero
#- estan tipo de dato "factor" y no Data (fecha), para cambiarlo uso as.Date
tabla1$fecha_sintomas=as.Date(tabla1$fecha_sintomas)
class(tabla1$fecha_sintomas)
## [1] "Date"
#-- geom_point() con linea de suavizado
p11=ggplot(tabla1,aes(x=fecha_sintomas, y= Casos))+geom_point()+theme_bw()
p11+geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'

p11+geom_smooth(method = "glm")
## `geom_smooth()` using formula 'y ~ x'

p11+geom_smooth(method = "loess")
## `geom_smooth()` using formula 'y ~ x'

p11+geom_smooth(method = "gam")
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

p11
