#Tarea 4: Cobertura por comuna.
##Ordenar ambos dataframe
testeocomuna= fread('https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto66/CoberturaPorComuna.csv')
positividadcomuna= fread("https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto65/PositividadPorComuna.csv")
testeocomuna = testeocomuna[!Comuna%like%"Desconocido",]
positividadcomuna = positividadcomuna[!Comuna%like%"Desconocido",]
fechastest=names(testeocomuna)
fechastest = fechastest [6:41]
fechaspos=names(positividadcomuna)
fechaspos = fechaspos [6:41]
testeocomuna = melt(data = testeocomuna,id.vars = c("Region","Comuna","Codigo comuna","Poblacion"),measure.vars = fechastest ,variable.name = "Fecha")
positividadcomuna = melt(data = positividadcomuna,id.vars = c("Region","Comuna","Codigo comuna","Poblacion"),measure.vars = fechaspos ,variable.name = "Fecha")
##sacar el porcentaje de test realizados por comuna
testeocomuna_p = testeocomuna[,.(suma_testeo=sum(value)), by=.(`Codigo comuna`,Comuna,Poblacion)]
testeocomuna_p=testeocomuna_p[, porcentaje_testeo:=((suma_testeo/Poblacion)*100), by=.(`Codigo comuna`, Comuna)]
#Mapa de porcentajes de test realizados por comuna respecto a cantidad de habitantes.
mapatest=merge(mapa_comunas,testeocomuna_p,by.x='codigo_comuna', by.y='Codigo comuna')
mapatest=as.data.table(mapatest)
mapatest=mutate_all(mapatest, ~replace(., is.na(.), 0))
mapatest=st_sf(mapatest)
bins=seq(0,1,0.07)
paleta = colorBin('Dark2', domain = mapatest$porcentaje_testeo, bins = bins)
labels = sprintf(
"<strong>%s</strong><br/>%g Porc. Testeo",
mapatest$Comuna, mapatest$porcentaje_testeo
) %>% lapply(htmltools::HTML)
leaflet(mapatest)%>%
addProviderTiles(provider = providers$OpenStreetMap.Mapnik)%>%
addPolygons(color = ~paleta(porcentaje_testeo),
weight = 1,
fillOpacity = 0.8,
label = labels)%>%
addLegend(pal = paleta, values = ~bins, opacity = 1,position = "bottomright",title ="Porc. testeo")
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +no_defs).
## Need '+proj=longlat +datum=WGS84'
## Warning in paleta(porcentaje_testeo): Some values were outside the color scale
## and will be treated as NA
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Dark2 is 8
## Returning the palette you asked for with that many colors
## Warning in paleta(porcentaje_testeo): Some values were outside the color scale
## and will be treated as NA
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Dark2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Dark2 is 8
## Returning the palette you asked for with that many colors