##GRAFICOS EN GGPLOT2

require(RSocrata)
## Loading required package: RSocrata
require(ggplot2)
## Loading required package: ggplot2
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_bogota=casos_ins[casos_ins$ciudad_de_ubicaci_n=="Bogotá D.C.",]

##Instalar las librerias

require(ggplot2)
library(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
casos_ins_bogota$edad=as.numeric(casos_ins_bogota$edad)

#Histograma de la Edad
p=ggplot(data=casos_ins_bogota,aes(x=edad))+geom_histogram(fill="yellow", color="red")+ylab("Casos")+xlab("Edad")+ylim(0,25000)+theme_update()+ggtitle("HISTOGRAMA COVID EN BOGOTA POR EDADES")
p
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplotly(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
### Histograma de casos reportados por COVID-19 en Bogotá D.C. según sexo.

condicion=which(casos_ins_bogota$sexo=="f")
casos_ins_bogota$sexo[condicion]="F"
table(casos_ins_bogota$sexo)
## 
##      F      M 
## 115155 110792
p2=ggplot(data=casos_ins_bogota, aes(x=edad, fill=sexo))+geom_histogram()+ylab("Casos")+xlab("Edad")+ylim(0,25000)+theme_update()+ggtitle("HISTOGRAMA COVID EN BOGOTA POR EDADES")+facet_grid(~sexo)+ylim(0,15000)
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.
p2
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplotly(p2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
### CAJAS POR EDAD Y ESTADO

p3=ggplot(data=casos_ins_bogota, aes(x=estado, y=edad, fill=estado))+geom_boxplot()+theme_bw()+ylab("Edad")+xlab("Estado")+ggtitle("Estado de contagiados por Covid-19 según edad en la ciudad de Bogotá D.C.")
p3

ggplotly(p3)
condi=casos_ins_bogota$estado=="N/A"
casos_ins_bogota$estado[condi]=NA
casos_ins_bogota$estado=as.factor(casos_ins_bogota$estado)

table(casos_ins_bogota$estado)
## 
## Asintomático    Fallecido        Grave         Leve     Moderado 
##        73070         5825          689       139454         6538
names(casos_ins_bogota)
##  [1] "id_de_caso"            "fecha_de_notificaci_n" "c_digo_divipola"      
##  [4] "ciudad_de_ubicaci_n"   "departamento"          "atenci_n"             
##  [7] "edad"                  "sexo"                  "tipo"                 
## [10] "estado"                "pa_s_de_procedencia"   "fis"                  
## [13] "fecha_diagnostico"     "fecha_recuperado"      "fecha_reporte_web"    
## [16] "tipo_recuperaci_n"     "codigo_departamento"   "codigo_pais"          
## [19] "pertenencia_etnica"    "nombre_grupo_etnico"   "fecha_de_muerte"
casos_ins_bogota_2=casos_ins_bogota[,c(7,10)]
casos_ins_bogota_3=na.omit(casos_ins_bogota_2)

casos_ins_bogota_3$estado2=factor(casos_ins_bogota_3$estado,levels = c("Asintomático","Leve","Moderado","Grave","Fallecido"))

p4=ggplot(data=casos_ins_bogota_3, aes(x=estado2, y=edad, fill=estado))+geom_boxplot()+theme_bw()+ylab("Edad")+xlab("Estado")+ggtitle("Estado de contagiados por Covid-19 según edad en la ciudad de Bogotá D.C.")
p4

ggplotly(p4)
###SERIES DE TIEMPO

class(casos_ins_bogota$fecha_de_notificaci_n)
## [1] "character"
casos_ins_bogota$fecha_not=as.Date(casos_ins_bogota$fecha_de_notificaci_n)

###Fecha de notificación de contagio por covid-19 en Bogotá D.C.
p4=ggplot(data=casos_ins_bogota, aes(x=fecha_not))+geom_bar(color="black", fill="white")+theme_bw()+ylab("# Casos")+xlab("Fecha de notificación")+ggtitle("Fechas de notificación de contagio de Covid-19 en Bogotá D.C.(2020)")
p4

ggplotly(p4)
###Estado de los contagiados por Covid-19 en Bogotá D.C.
p5=ggplot(data=casos_ins_bogota_3, aes(x=estado2))+geom_bar(color="red", fill="yellow")+theme_bw()+ylim(0,150000)+xlab("Estado")+ylab("#Casos")+ggtitle("Estado de contagiados por Covid-19 en Bogotá D.C.")
p5

ggplotly(p5)
###Fecha de inicio de síntomas asociados a covid-19 en Bogotá D.C.

casos_ins_bogota$fis=as.Date(casos_ins_bogota$fis)

p6=ggplot(data=casos_ins_bogota, aes(x=fis))+geom_bar(color="black", fill="white")+theme_bw()+ylab("# Casos")+xlab("Fecha de notificación")+ggtitle("Fechas de notificación de contagio de Covid-19 en Bogotá D.C.(2020)")
p6
## Warning: Removed 73070 rows containing non-finite values (stat_count).

ggplotly(p6)
## Warning: Removed 73070 rows containing non-finite values (stat_count).
####

class(casos_ins_bogota$fis)
## [1] "Date"
tabla1=table(casos_ins_bogota$fis)
tabla1=data.frame(tabla1)
names(tabla1)=c("Fecha_Sintomas","Casos")
tabla1
##     Fecha_Sintomas Casos
## 1       2020-02-27     1
## 2       2020-02-28     1
## 3       2020-03-01     3
## 4       2020-03-04     1
## 5       2020-03-05     5
## 6       2020-03-06     6
## 7       2020-03-07    10
## 8       2020-03-08     5
## 9       2020-03-09     5
## 10      2020-03-10    11
## 11      2020-03-11    15
## 12      2020-03-12    19
## 13      2020-03-13    26
## 14      2020-03-14    34
## 15      2020-03-15    54
## 16      2020-03-16    62
## 17      2020-03-17    59
## 18      2020-03-18    53
## 19      2020-03-19    86
## 20      2020-03-20    84
## 21      2020-03-21    79
## 22      2020-03-22    55
## 23      2020-03-23    62
## 24      2020-03-24    48
## 25      2020-03-25    69
## 26      2020-03-26    68
## 27      2020-03-27    77
## 28      2020-03-28    70
## 29      2020-03-29    61
## 30      2020-03-30    61
## 31      2020-03-31    46
## 32      2020-04-01    87
## 33      2020-04-02    59
## 34      2020-04-03    56
## 35      2020-04-04    71
## 36      2020-04-05    67
## 37      2020-04-06    91
## 38      2020-04-07    57
## 39      2020-04-08    71
## 40      2020-04-09    63
## 41      2020-04-10   102
## 42      2020-04-11    66
## 43      2020-04-12    70
## 44      2020-04-13    60
## 45      2020-04-14    69
## 46      2020-04-15    84
## 47      2020-04-16    72
## 48      2020-04-17    74
## 49      2020-04-18    92
## 50      2020-04-19    76
## 51      2020-04-20   110
## 52      2020-04-21    77
## 53      2020-04-22    75
## 54      2020-04-23    96
## 55      2020-04-24    86
## 56      2020-04-25    98
## 57      2020-04-26   123
## 58      2020-04-27   104
## 59      2020-04-28   146
## 60      2020-04-29   111
## 61      2020-04-30   113
## 62      2020-05-01   208
## 63      2020-05-02   152
## 64      2020-05-03   149
## 65      2020-05-04   150
## 66      2020-05-05   197
## 67      2020-05-06   229
## 68      2020-05-07   165
## 69      2020-05-08   216
## 70      2020-05-09   219
## 71      2020-05-10   327
## 72      2020-05-11   274
## 73      2020-05-12   255
## 74      2020-05-13   258
## 75      2020-05-14   248
## 76      2020-05-15   264
## 77      2020-05-16   270
## 78      2020-05-17   277
## 79      2020-05-18   368
## 80      2020-05-19   384
## 81      2020-05-20   424
## 82      2020-05-21   288
## 83      2020-05-22   363
## 84      2020-05-23   356
## 85      2020-05-24   311
## 86      2020-05-25   404
## 87      2020-05-26   305
## 88      2020-05-27   351
## 89      2020-05-28   343
## 90      2020-05-29   337
## 91      2020-05-30   323
## 92      2020-05-31   326
## 93      2020-06-01   577
## 94      2020-06-02   470
## 95      2020-06-03   410
## 96      2020-06-04   406
## 97      2020-06-05   500
## 98      2020-06-06   496
## 99      2020-06-07   378
## 100     2020-06-08   514
## 101     2020-06-09   488
## 102     2020-06-10   676
## 103     2020-06-11   559
## 104     2020-06-12   522
## 105     2020-06-13   523
## 106     2020-06-14   545
## 107     2020-06-15   713
## 108     2020-06-16   767
## 109     2020-06-17   899
## 110     2020-06-18  1017
## 111     2020-06-19  1020
## 112     2020-06-20  1200
## 113     2020-06-21   730
## 114     2020-06-22   802
## 115     2020-06-23  1035
## 116     2020-06-24   974
## 117     2020-06-25  1129
## 118     2020-06-26  1380
## 119     2020-06-27  1450
## 120     2020-06-28  1150
## 121     2020-06-29  1051
## 122     2020-06-30  1270
## 123     2020-07-01  1775
## 124     2020-07-02  1380
## 125     2020-07-03  1650
## 126     2020-07-04  1564
## 127     2020-07-05  1515
## 128     2020-07-06  1703
## 129     2020-07-07  1691
## 130     2020-07-08  1822
## 131     2020-07-09  1682
## 132     2020-07-10  2517
## 133     2020-07-11  1692
## 134     2020-07-12  1885
## 135     2020-07-13  2202
## 136     2020-07-14  2140
## 137     2020-07-15  2058
## 138     2020-07-16  2312
## 139     2020-07-17  2368
## 140     2020-07-18  2461
## 141     2020-07-19  2416
## 142     2020-07-20  2674
## 143     2020-07-21  2699
## 144     2020-07-22  3040
## 145     2020-07-23  2709
## 146     2020-07-24  3055
## 147     2020-07-25  2638
## 148     2020-07-26  2053
## 149     2020-07-27  3277
## 150     2020-07-28  2341
## 151     2020-07-29  2332
## 152     2020-07-30  2181
## 153     2020-07-31  2130
## 154     2020-08-01  2847
## 155     2020-08-02  2051
## 156     2020-08-03  2447
## 157     2020-08-04  2455
## 158     2020-08-05  2834
## 159     2020-08-06  2790
## 160     2020-08-07  2144
## 161     2020-08-08  2551
## 162     2020-08-09  1947
## 163     2020-08-10  2730
## 164     2020-08-11  1946
## 165     2020-08-12  1707
## 166     2020-08-13  1307
## 167     2020-08-14  1354
## 168     2020-08-15  1358
## 169     2020-08-16  1200
## 170     2020-08-17  1277
## 171     2020-08-18  1565
## 172     2020-08-19  1866
## 173     2020-08-20  1895
## 174     2020-08-21  1299
## 175     2020-08-22   899
## 176     2020-08-23   768
## 177     2020-08-24   885
## 178     2020-08-25   906
## 179     2020-08-26   764
## 180     2020-08-27   634
## 181     2020-08-28   626
## 182     2020-08-29   483
## 183     2020-08-30   404
## 184     2020-08-31   368
## 185     2020-09-01   274
## 186     2020-09-02   129
## 187     2020-09-03    37
## 188     2020-09-04    14
tabla1$Fecha_Sintomas=as.Date(tabla1$Fecha_Sintomas)

p7=ggplot(tabla1,aes(x=Fecha_Sintomas,y=Casos))+geom_point()+geom_smooth(method = "gam")
p7
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

ggplotly(p7)
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'