Last active
June 26, 2019 17:21
-
-
Save lucassmacedo/b977568960d43833b68b455816fdf1b7 to your computer and use it in GitHub Desktop.
R Map Plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sf) | |
library(ggplot2) | |
library("tmap") | |
library("tmaptools") | |
library (leaflet) | |
library (dplyr) | |
options (scipen =999) | |
library(DBI) | |
con <- dbConnect(RMariaDB::MariaDB(), host = "xx", user = "xx",dbname="xx",password = "xx") | |
# You can fetch all results: | |
mydata <- dbSendQuery(con, "select municipio as COD_IBGE, sum(total) as total | |
from vendas | |
group by COD_IBGE") | |
mydata <- dbSendQuery(con, "select municipio as COD_IBGE, sum(total) as total, count(*) as qtd | |
from vendas_2019 | |
where filial = '01' | |
group by COD_IBGE") | |
mymap = st_read("Brasil/MUNICIPIOS_polígonos.shp", stringsAsFactors=FALSE) | |
map_and_data <- left_join(mymap, dbFetch(mydata)) | |
geom_sf(aes(fill = Faturamento)) | |
tm_shape(map_and_data) + | |
tm_polygons(col = "total", | |
id= "NOME_MUNI", | |
style= "quantile", | |
n=10, | |
palette = c("#ffd92f", "#a6d854", "#6bb2db", "#4fa35f","#db5727", "#ff9b30"), | |
textNA = "Sem dados", | |
palette = "Greens", | |
popup.vars = c("Total: " = 'total'), | |
border.alpha = .5, | |
title="Legenda") + | |
tm_layout(scale = 1, legend.bg.color = "white", legend.bg.alpha = 0.3) + | |
tm_facets(nrow = 1, sync = TRUE) + | |
tm_legend(outside = TRUE) | |
tmap_mode("view") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment