How to solve problem with reading a shapefile in R

How do I show a map that is in shapefile format that is in a zip file. The file is this:

https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip

I tried to do the following:

library(rgdal)

temp <- tempfile()
download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp)
data <- readOGR(unz(temp, "41MUE250GC_SIR.shp"))

But it did not work.

The map will look like the one below:

enter image description here

Probably better to use sf than rgdal it is just a better package for working with spatial data.

You could do this:

library(sf)

temp <- tempfile()
download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp)
data <- st_read(unz(temp, "41MUE250GC_SIR.shp"))

# Unzip the contents of the temp and save in temp2
temp2 <- tempfile()
unzip(zipfile = temp, exdir = temp2)

# Read using sf
municipios <- st_read(temp2)

Thanks for answering @williaml ! And to plot a graph similar to the one in the question, how do I do it? I made plot(municipios), but it shows two maps.

plot(municipios)

Use the $ for a particular column e.g. plot(municipios$your_column).

More info: 5. Plotting Simple Features • sf

Otherwise, you can use ggplot2, or other packages including leaflet, tmap, mapsf.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.