Zoom region that contains points on a map in R

The code below generates a map together with points that refer to the coordinates of some houses, but note that it is not possible to see the location of these houses very well. So, is there any way to improve this graph, making it possible, for example, to zoom the map closer to the region of the points?

Generate map

library(rgdal)
        
temp <- tempfile()
temp2 <- tempfile()
        download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp)

      
unzip(zipfile = temp, exdir = temp2)
shp <- readOGR(temp2)
shp_subset <- shp[shp$NM_MUNICIP == "CASTRO",]
plot(shp_subset)

Generate Points

  Points_properties<-structure(list(Latitude = c(-24.781624, -24.775017, -24.769196, 
     -24.761741, -24.752019, -24.748008, -24.737312, -24.744718, -24.751996, 
     -24.724589, -24.8004, -24.796899, -24.795041, -24.780501, -24.763376, 
     -24.801715, -24.728005, -24.737845, -24.743485, -24.742601, -24.766422, 
     -24.767525, -24.775631, -24.792703, -24.790994, -24.787275, -24.795902, 
     -24.785587, -24.787558, -24.799524), Longitude = c(-49.937369, 
     -49.950576, -49.927608, -49.92762, -49.920608, -49.927707, -49.922095, 
     -49.915438, -49.910843, -49.899478, -49.901775, -49.89364, -49.925657, 
     -49.893193, -49.94081, -49.911967, -49.893358, -49.903904, -49.906435, 
     -49.927951, -49.939603, -49.941541, -49.94455, -49.929797, -49.92141, 
     -49.915141, -49.91042, -49.904772, -49.894034, -49.86651), cluster = c("1", "1", 
     "1", "1", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1", "1", 
     "1", "2", "2", "2", "2", "1", "1", "1", "1", "1", "1", "1", "1", 
     "1", "1")), row.names = c(NA, -30L), class = c("tbl_df", "tbl", 
     "data.frame"))

    points(x = Points_properties$Longitude, y= Points_properties$Latitude, col = "black",pch = 16,cex=1)

Output
enter image description here

you can use raster::crop as one approach. Here I defined a linear mapping on a z factor where z=0 represents the original bounds, and z=1 represents the bounds of the points you provide, you can pick your preferred z between 0 and 1 therefore

marsize <- .2
par(mar = rep(marsize,4))
z <- .3
shp_subset_cropped <- raster::crop(shp_subset,
          extent(c(
            (extent(shp_subset)@"xmin"*(1-z)+z*min(Points_properties$Longitude)),
            (extent(shp_subset)@"xmax"*(1-z)+z*max(Points_properties$Longitude)),
            (extent(shp_subset)@"ymin"*(1-z)+z*min(Points_properties$Latitude)),
            (extent(shp_subset)@"ymax"*(1-z)+z*max(Points_properties$Latitude))
          )))
plot(shp_subset_cropped)
points(x = Points_properties$Longitude,
       y= Points_properties$Latitude, col = "black",pch = 16,cex=1)

image

1 Like

Exactly what I wanted @nirgrahamuk, thank you so much! I have another question about this, I don't know if it's better to ask a new question or take advantage of this question, what do you think? Making a brief explanation, see that I have 30 coordinates (houses), but I have another column in Points_properties, which shows which cluster determining house is in, if it is in cluster 1 or cluster 2. And I wanted to differentiate these clusters in this map that you made . Is it possible? If so, can I ask a new question about it. I will insert a figure to exemplify. Thank you again!

Imagem1

your original example contained these cluster label, you could make a simple addition after the plot(shp_subset_cropped) stage:

plot(shp_subset_cropped)

pp1 <- filter(Points_properties,
              cluster==1)
pp2 <- filter(Points_properties,
              cluster==2)

points(x = pp1$Longitude,
       y= pp1$Latitude, col = "red",pch = 16,cex=1)
points(x = pp2$Longitude,
       y= pp2$Latitude, col = "blue",pch = 16,cex=1)

Thanks for reply @nirgrahamuk! But when I do pp1, appears: Error in filter(Points_properties, cluster == 1) : object 'cluster' not found

you should review your code to see at what point you lost cluster from the points properties you started out with

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.