Yana Borges
2020-Mar-24 19:44 UTC
[R] How to read a shapefile with areas other than the one used by default in the spplot?
Hello! I send the same question I asked in the stackoverflow, if you don't mind. It is possible that it is something simple, but I really did not succeed. I need to make a map as it is in the script I send below. The shapefile I am using, as well as the data, are attached. My problem: I want the divisions by Regional Health (RS), and not by municipality, which are 399. There are 22 Health Regionals, but I can't identify them on the map. In the shp file there is this subdivision, but I was unable to insert it that way. How do I read by RS and not by municipality (default)? I used the packages ggplot2, factoextra and rgdal Thank you! prec <- readxl::read_xlsx('Cluster\\municipios.xlsx', sheet "Obito0a6dias2",col_names = T) tard <- readxl::read_xlsx('Cluster\\municipios.xlsx', sheet "Obito7a27dias2",col_names = T) pop <- readxl::read_xlsx('Cluster\\municipios.xlsx', sheet "pop2",col_names = T) # TAXAS taxa.prec <- data.frame(prec[1:22,2:18]/pop[1:22,2:18]*1000,row.names = prec$mun) taxa.tard <- data.frame(tard[1:22,2:18]/pop[1:22,2:18]*1000,row.names = prec$mun) `# head(taxa.prec) ################### Dados ###################### txp <- txt <- res.hc <- grupo <- list() dados <- taxa.grupos <- plot <- list() for (i in 1:ncol(taxa.prec)) { txp[[i]] <- scale(taxa.prec[,i]) txt[[i]] <- scale(taxa.tard[,i]) res.hc[[i]] <- hclust(dist(data.frame(txp[[i]], txt[[i]], row.names = prec$mun)), method = "ward.D2") grupo[[i]] <- cutree(res.hc[[i]], k = 4) dados[[i]] <- data.frame(RS = 1:22,taxap = taxa.prec[,i], taxat = taxa.tard[,i], Grupo=factor(grupo[[i]]), row.names = prec$mun) taxa.grupos[[i]] <- aggregate(cbind(taxap, taxat) ~ grupo[[i]], dados[[i]], mean) taxa.grupos[[i]]$media.pt <- apply(taxa.grupos[[i]][2:3], 1, mean)} names(dados) <- 2000:2016 mapa <- maptools::readShapePoly('Cluster\\PR\\rs_pr\\RS.shp') gp <- list(gp1=list(),gp2=list(),gp3=list(),gp4=list())for (i in 1:17) { for (j in 1:4) { gp$gp1[[i]] <- which(dados[[i]]$Grupo == 1) gp$gp2[[i]] <- which(dados[[i]]$Grupo == 2) gp$gp3[[i]] <- which(dados[[i]]$Grupo == 3) gp$gp4[[i]] <- which(dados[[i]]$Grupo == 4) }} aux <- matrix(0, ncol=17, nrow=399) for (i in 1:17) {for (j in 1:4) { aux[,i][mapa$RS %in% gp[[j]][[i]]] <- taxa.grupos[[i]]$media.pt[j]}} mapa at data[6:22] <- aux ############# Mapas em cores#### corDegrade <- colorRampPalette(c("#FFFF99", "orange","red", "darkred")) x11()spplot(mapa, zcol=c(6:22), col.regions =corDegrade(16), layout(matrix(c(1:20),nrow = 5,ncol = 4, byrow = TRUE)), names.attr = c(2000:2016))
Bert Gunter
2020-Mar-25 15:20 UTC
[R] How to read a shapefile with areas other than the one used by default in the spplot?
1. Wrong list. You should post on r-sig-geo instead. 2. As you use ggplot, why aren't you using ggmap() ? Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Mar 25, 2020 at 6:34 AM Yana Borges <borges.yana at gmail.com> wrote:> > Hello! > I send the same question I asked in the stackoverflow, if you don't mind. > It is possible that it is something simple, but I really did not succeed. > > I need to make a map as it is in the script I send below. The shapefile I > am using, as well as the data, are attached. > My problem: I want the divisions by Regional Health (RS), and not by > municipality, which are 399. There are 22 Health Regionals, but I can't > identify them on the map. In the shp file there is this subdivision, but I > was unable to insert it that way. > How do I read by RS and not by municipality (default)? > > I used the packages ggplot2, factoextra and rgdal > > Thank you! > > prec <- readxl::read_xlsx('Cluster\\municipios.xlsx', sheet > "Obito0a6dias2",col_names = T) > tard <- readxl::read_xlsx('Cluster\\municipios.xlsx', sheet > "Obito7a27dias2",col_names = T) > pop <- readxl::read_xlsx('Cluster\\municipios.xlsx', sheet > "pop2",col_names = T) > # TAXAS > taxa.prec <- data.frame(prec[1:22,2:18]/pop[1:22,2:18]*1000,row.names > = prec$mun) > taxa.tard <- data.frame(tard[1:22,2:18]/pop[1:22,2:18]*1000,row.names > = prec$mun) > `# head(taxa.prec) > ################### Dados ###################### > txp <- txt <- res.hc <- grupo <- list() > dados <- taxa.grupos <- plot <- list() > for (i in 1:ncol(taxa.prec)) { > txp[[i]] <- scale(taxa.prec[,i]) > txt[[i]] <- scale(taxa.tard[,i]) > res.hc[[i]] <- hclust(dist(data.frame(txp[[i]], > txt[[i]], > row.names = prec$mun)), > method = "ward.D2") > grupo[[i]] <- cutree(res.hc[[i]], k = 4) > dados[[i]] <- data.frame(RS = 1:22,taxap = taxa.prec[,i], > taxat = taxa.tard[,i], > Grupo=factor(grupo[[i]]), > row.names = prec$mun) > taxa.grupos[[i]] <- aggregate(cbind(taxap, taxat) ~ grupo[[i]], > dados[[i]], mean) > taxa.grupos[[i]]$media.pt <- apply(taxa.grupos[[i]][2:3], 1, mean)} > names(dados) <- 2000:2016 > > mapa <- maptools::readShapePoly('Cluster\\PR\\rs_pr\\RS.shp') > > gp <- list(gp1=list(),gp2=list(),gp3=list(),gp4=list())for (i in 1:17) { > for (j in 1:4) { > gp$gp1[[i]] <- which(dados[[i]]$Grupo == 1) > gp$gp2[[i]] <- which(dados[[i]]$Grupo == 2) > gp$gp3[[i]] <- which(dados[[i]]$Grupo == 3) > gp$gp4[[i]] <- which(dados[[i]]$Grupo == 4) }} > > aux <- matrix(0, ncol=17, nrow=399) > for (i in 1:17) {for (j in 1:4) { > aux[,i][mapa$RS %in% gp[[j]][[i]]] <- taxa.grupos[[i]]$media.pt[j]}} > mapa at data[6:22] <- aux > > ############# Mapas em cores#### > corDegrade <- colorRampPalette(c("#FFFF99", "orange","red", "darkred")) > x11()spplot(mapa, zcol=c(6:22), col.regions =corDegrade(16), > layout(matrix(c(1:20),nrow = 5,ncol = 4, byrow = TRUE)), > names.attr = c(2000:2016)) > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.