lines crossing in a map with grids (ggplot) - r

I've been having difficulties with a map I created in R. I'm trying to make a map with a grid where in each square, there's a value corresponding to a dataframe. So far, I made the shapefile and the script following some tutorials and posts from here (Download), but the final result got kinda weird.
library(xlsx)
library(ggplot2)
library(sp)
library(raster)
library(plyr)
library(dplyr)
library(tidyr)
library(sp)
library(raster)
library(rgeos)
library(rgbif)
library(viridis)
library(gridExtra)
library(rasterVis)
library(ggplot2)
library(maps)
library(rgdal)
br <- readOGR(choose.files(), "brgrid")
plot(br)
class(br)
str(br#data)
br#data$id <- rownames(br#data)
br.df <- fortify(br)
br.df <- join(br.df, br#data, by="id")
str(br.df)
tail(br.df)
names(br.df)
tail(br.df$id)
dados <- read.xlsx("ptsgrid.xlsx",6)
names(dados)
br.df <- merge(br.df, dados, by.x="id", by.y="id", all.x=T, a..ly=F)
str(br.df)
ggp <- ggplot(data=br.df, aes(x=long, y=lat, group=group))
ggp <- ggp + geom_polygon(aes(fill=value)) # draw polygons
ggp <- ggp + geom_path(color="grey", linestyle=1) # draw boundaries
ggp <- ggp + coord_equal()
ggp <- ggp + scale_fill_gradient(low = "#ffffcc", high = "#ff4444",
space = "Lab", na.value = "grey50",
guide = "colourbar")
print(ggp)
I want to know why the lines got crossed in the map, and how to fix it. Also, I wanna know if it's possible to omit some squares (I only have interest in coastal region, so it'll look better if I omit the rest). And finally, I don't know why the map boundaries got "overlayed" (maybe a projection issue?).

Welcome to Stack Overflow! What have you done so far/what does your code look like? I will share with you a map/code I made that functions as I intended, so you can replace values in my code with what you need for your needs/with your data Below is code for a map I created in R for income in the United States:
library(maps)
library(ggplot2)
usa=map_data("state")
ggplot(usa)+geom_polygon(aes(x=long,y=lat,group=group,fill=region),color="white")+coord_fixed(1.3)+guides(fill=FALSE,color=FALSE)
The result of the above code:

I downloaded your data set. I did not examine your code. But I think when you created br.df using merge(), order got messed up. In your code, I am talking about this line (br.df<-merge(br.df, dados, by.x="id", by.y="id", all.x=T, a..ly=F). You may want to check this question. You wanted to combine all data sets. But you do not have to do that.
In your data set, dados, you have 148 data points. But you have 150 grids in your map data. So I modified your EXCEL data; I added id = 0 and id = 149. Each data point has 0 and 495, respectively. As long as there is a common column name (in this case, id), you can do the following. I used geom_cartogram() in the ggalt package. The second geom_cartogram() is adding colors to the grids.
library(rgdal)
library(ggplot2)
library(ggalt)
library(readxl)
# Create a map data
foo <- readOGR(dsn = "brgrid.shp")
mygrid <- fortify(foo)
# Import the EXCEL data
dados <- read_excel("ptsgrid.xlsx", sheet = 6)
ggplot() +
geom_cartogram(data = mygrid, map = mygrid,
aes(x = long, y = lat, map_id = id),
color = "black", alpha = 0.5) +
geom_cartogram(data = dados, map = mygrid,
aes(fill = value, map_id = id)) +
scale_fill_gradient(low = "#ffffcc", high = "#ff4444",
space = "Lab", na.value = "grey50",
guide = "colourbar")

Related

st_centroid renders all labels on the same point

I'm trying to display labels on GIS polygon features in R using the st_centroid function in the sf library. Unfortunately, while the head() function seems to show that each polygon has different x and y coordinates associated with it, all labels get rendered overlapping at a single point on the map (which is apparently the centroid of one particular polygon). What am I doing wrong here?
Current code setup:
library("ggplot2")
library("sf")
sf::sf_use_s2(FALSE) #makes centroids not break
world <- st_read("C:/prgrm/gis/source/10m_land_and_islands.shp")
prov <- st_read("C:/prgrm/gis/edited ncm/ncm_provinces.shp")
prov <- cbind(prov, st_coordinates(st_centroid(prov))) #attaches centroids to 'prov' dataset
head(prov)
ggplot(data = world) +
geom_sf() +
geom_sf(data=prov, aes(fill="blue")) +
geom_text(data=prov, aes(X,Y, label=provname_r), size=5) +
coord_sf(xlim=c(-2000000,1000000),ylim=c(-1500000, 3000000), crs=st_crs(3310))
You may be better off with specifying the centroid placement via fun.geometry argument of the geom_sf_text() call / by the way the default is sf::st_point_on_surface() - which is a good default as it makes sure that the label is not placed inside a hole, should the polygon have one.
Consider this example, using the well known & much loved nc.shp shapefile that ships with {sf}.
library(sf)
library(ggplot2)
# in place of your world dataset
shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package
# in place of your prov dataset
ashe <- shape[1, ]
ggplot(data = shape) +
geom_sf() +
geom_sf(data = ashe, fill = "blue") +
geom_sf_text(data = ashe,
aes(label = NAME),
color = "red",
fun.geometry = st_centroid)

ggplot maps - unwanted horizontal lines when using coord_map

I've problem with projecting properly map of Europe when I use coord_map in ggplot package. The code below gives me a weird and unwanted horizontal lines in random places. Does anyone know how to overcome this problem?
I don't want to use coord_quickmap nor coord_cartesian because I want to preserve straight lines of countries.
library(ggplot2)
map.world <- map_data("world")
ggplot(map.world, aes(x = long, y = lat)) +
geom_polygon(mapping = aes(x = long, y = lat, group = group), fill = "#B3B1B5", color = "#D9D8DA",size = 0.4) +
theme_minimal() +
theme(axis.text = element_blank(), text = element_blank(), panel.grid = element_blank()) +
coord_map(xlim = c(-27,36), ylim = c(34,67))
There are two ways to work around your problem:
The straightforward way
This way just needs a little tweaking of your code (note that I use magrittr's forward pipes):
library(maps)
library(magrittr)
library(maptools)
library(broom)
library(ggplot2)
europe <- maps::map("world", fill=TRUE, plot=FALSE) %>%
maptools::pruneMap(xlim = c(-27,36), ylim = c(34,67))
ggplot(data= broom::tidy(europe)) +
geom_polygon(mapping = aes(x = long, y = lat, group = group),
fill = "#B3B1B5", color = "#D9D8DA",size = 0.4) +
theme_void() +
coord_map()
The "owin/extent" approach
Another way to work around your problem can be done by using owin objects. This kind of objects will allow you to create a spatial window. Then you can represent only the intersection of such window over the world map.
Using this approach your code will be something as follows (also using magrittr's forward pipes and setting up a general CRS)
library(maps)
library(magrittr)
library(spatstat)
library(maptools)
library(raster)
library(broom)
library(ggplot2)
#Defining a general EPSG so there won't be any superposition problems
epsg <- "+proj=longlat +datum=WGS84 +no_defs"
#Using the original maps package, then converting map into SpatialPolygons object
map.world <- maps::map("world", fill=TRUE) %$%
maptools::map2SpatialPolygons(., IDs=names,proj4string=CRS(epsg))
#In order to keep the names of the countries we create the following data.frame
country.labs <- sapply(slot(map.world, "polygons"), function(x) slot(x, "ID")) %>%
data.frame( ID=1:length(map.world), name=., row.names = .)
#We convert object map.world into a SpatialPolygonsDataFrame object
map.world.SPDF <- sp::SpatialPolygonsDataFrame(map.world, country.labs)
#Creating owin object using your zooming coordinates
#This step always requires to load packages 'spatstat' and 'maptools'
zoom <- as(spatstat::as.owin(c(-27,36,34,67)), "SpatialPolygons")
raster::projection(zoom)=epsg
#Storing intersection between 'zoom' and 'world.map'
europe <- raster::intersect(map.world.SPDF, zoom)
#*country names of object europe can be accessed via europe#data
#Representing object 'europe' using broom::tidy to create a data.frame object
ggplot() +
geom_polygon(data = broom::tidy(europe, region="name"),
mapping = aes(x = long, y = lat, group = group),
fill = "#B3B1B5", color = "#D9D8DA",size = 0.4) +
theme_void() +
coord_map()
#*country names after tidying 'europe' this way are in a new column called 'id'
Depending on what you are doing, you may want to use
zoom <- as(raster::extent(c(-27,36,34,67)), "SpatialPolygons")
To create an extent object instead of an owin object (The result here is going to be the same)
The result obtained with any method is displayed below
In case you need to do different zooms you can easily wrap any of the alternatives up in a function.
I hope it helps
BONUS BALL: It might be interesting for you to check out the tmap package

the borders function in ggplot did not work as expected?

I want to plot the (spatial) correlation pattern between two variables and overlapped by countries borders. I use borders() function with ggplot but the xlim and ylim didn't give the exact limit I expect, instead, they give all the country. I want to have the exact limits to overlap the data.
library(ncdf4)
library(ggplot2)
library(reshape)
library(maps)
library(stringr)
library(reshape2)
library(mapdata)
library(maptools)
ncfile <- nc_open("/Volumes/KIT/CHIRPS/index/correlation/correlation.amm.allindex.nc")
lon <- ncvar_get(ncfile, "lon")
lat <- ncvar_get(ncfile, "lat")
pr <- ncvar_get(ncfile, "prptotcorrel")
ret <- list ("lat"=lat, "lon"=lon,"pr"= pr)
##
str(ret)
######## melt function for pr
melt_pr <- function(L) {
dimnames(L$pr) <- list(lon = L$lon, lat = L$lat)
rett <- melt(L$pr, value.name ="pr")
}
######
mpr <- melt_pr(ret)
head(mpr)
#
xlims=range(mpr$lon); xlims
ylims=range(mpr$lat); ylims
gcorrelation=ggplot(data = mpr, aes(x = lon, y = lat, fill = pr)) +
geom_tile() +
borders('world', xlim=xlims, ylim=ylims, colour='black', size=.2) +
coord_fixed(xlim=xlims, ylim=ylims) +
labs(x = "Longitude", y = "Latitude") +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), na.value = NA, name="correlation") +
theme_bw() +
coord_fixed(1.3)
gcorrelation
I expect to have country borders that fit exactly the data.
Here is what I'd like the plot to look like. Here is the image with the countries; I want just to overlap with the exact limit of shaded data.
Welcome to stack overflow. Without having a sample of your data (as code), it will be hard to help. I believe that coord_cartesian() will do what you want instead of coord_fixed()
Instead of coord_fixed() or coord_cartesian(), use the xlim and ylim parameters of coord_map().
coord_map() projects a portion of the earth, which is approximately spherical, onto a flat 2D plane using any projection defined by the mapproj package.
(As #yake84 suggested, it would be best to present a simplified version of your problem that contains code and data so people can run and test their solutions.)
Taking a step back, if you are developing fresh code, I'd avoid outdated packages like reshape, reshape2, and some of the spatial functions. Instead build on their replacements (often developed by the same people) such as tidyr, sf, and the associated functions in ggplot2, such as ggsf().
Hi #yake84 and #wibeasley, sorry I'm pretty new on Stack overflow, thanks for your suggestions, coord_map() gave the right expected map. Thanks also for the packages suggestion.

How can I edit the place of the legends with ggplot2

I am having trouble in separating the legends in a ggplot2 graph with multiple layers. What my plot does is to fill different municipalities according to the number of textile companies present there and I also plot the plant localization with geom_point. My guess is to use aes.override() somehow, but I haven't been able to do this still. The solutions that I have read do not deal with a different variable for the plots detailed in the aes() of geom_point().
If you want to test the code below, you could download the shapefile for the brazilian municipalities here, use readOGR and fortify, then choose to fill the municipalities with your preference with fill and set arbitrarily random points within Brazil for geom_point() creating a different variable, such as lat_plant and long_plant below. The region column below details brazilian regions -- in this case, the "1" details the northern region of Brazil.
The Code
#setting the ggplot
library(ggplot2)
gg2 < -ggplot(data = out[out$region =="1",],
aes(x = long, y = lat, group = group, fill = as.factor(companies))) +
geom_polygon() +
ggtitle("title") +
scale_fill_discrete(name = "Number of Textile Companies") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(legend.text = element_text(size = 12),
legend.title = element_text(colour = "blue", size = 16, face = "bold"))
#graph output
gg2 +
geom_point(data = out[out$region =="1",], aes(x = long_plant, y = lat_plant), color = "red")
What I am getting as legend is this:
And I would like to separate it, detailing that the dots as localizations and the colors as the filling for the number of textile companies in the region.
I leave another option for you. hmgeiger treated the number of textile companies as factor. But, I rather treated the variable as a continuous variable. Since there is NO reproducible data, I created a sample data by myself. Here, I created random samples uing longitude and latitude of Brazil, and made sure that some data points stay in Brazil. whatever2 contains data points staying in Brazil. I did a bit of trick here as well. I added a new column called Factory location. This is the dummy variable for adding color to data points in the final graphic. hmgeiger created Dummy.var that contains characters for you. I rather left "" in this column since you may not want to see any text in legend.
For your legend issue, as Antonio mentioned and hmgeiger did, you need to add color in aes() in geom_point(). This solves it. I did a bit more thing for you. If you do not know how many factories exist in each municipal, you need to count the number of factories. I did the job using poly.count() in the GISTools package and created another data frame that contains the numbers of factories in each municipal.
When I drew the map, I had three layers. One is for the polygons and another for filling the polygons with colors. They are done with geom_cartogram() from the ggalt package. The key thing is that you need to have a common key column for map_id. id in the first geom_cartogram() and ind in the second geom_cartogram() are identical information. In geom_point() you need color in aes(). The legend has a continuous bar for the number of factories and a single dot for factory location. No text exists next to it. So this makes the legend tidy, I think.
library(raster)
library(tidyverse)
library(GISTools)
library(RColorBrewer)
library(ggalt)
library(ggthemes)
# Get polygon data for Brazil
brazil <- getData("GADM", country = "brazil", level = 1)
mymap <- fortify(brazil)
# Create dummy data staying in the polygons
# For more information: https://stackoverflow.com/questions/47696382/removing-data-outside-country-map-boundary-in-r/47699405#47699405
set.seed(123)
mydata <- data.frame(long = runif(200, min = quantile(mymap$long)[1], max = quantile(mymap$long)[4]),
lat = runif(200, min = quantile(mymap$lat)[1], max = quantile(mymap$lat)[4]),
factory = paste("factory ", 1:200, sep = ""),
stringsAsFactors = FALSE)
spdf <- SpatialPointsDataFrame(coords = mydata[, c("long", "lat")], data = mydata,
proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
whatever <- spdf[!is.na(over(spdf, as(brazil, "SpatialPolygons"))), ]
whatever2 <- as.data.frame(whatever) %>%
mutate(`Factory location` = "")
# Now I check how many data points (factories) exist in each polygon
# and create a data frame
factory.num <- poly.counts(pts = whatever, polys = brazil)
factory.num <- stack(factory.num)
ggplot() +
geom_cartogram(data = mymap, aes(x = long, y = lat, map_id = id),
map = mymap) +
geom_cartogram(data = factory.num, aes(fill = values, map_id = ind),
map = mymap) +
geom_point(data = whatever2, aes(x = long, y = lat, color = `Factory location`)) +
scale_fill_gradientn(name = "Number of factories", colours = brewer.pal(5, "Greens")) +
coord_map() +
theme_map()
FYI, the link you posted to download the shape file is quite slow, at least to download to a US computer.
This link has downloads that work a lot better, and also shows how to read in shape data: https://dioferrari.wordpress.com/2014/11/27/plotting-maps-using-r-example-with-brazilian-municipal-level-data/
I made an example using the regions rather than municipalities data to keep it simple.
Data I used available for download here: https://drive.google.com/file/d/0B64xLcn8DZfwakNMbHFLQWo4YzA/view?usp=sharing
#Load libraries.
library(rgeos)
library(rgdal)
library(ggplot2)
#Read in and format map data.
regions_OGR <- readOGR(dsn="/Users/hmgeiger/Downloads/regioes_2010",
layer = "regioes_2010")
map_regions <- spTransform(regions_OGR,CRS("+proj=longlat +datum=WGS84"))
map_regions_fortified <- fortify(map_regions)
#We make there be 0, 1, or 3 textile companies.
#map_regions_fortified is in order by ID (region).
#So, we add a column with the number of textile companies
#repeated the right number of times for how many of each region there is.
num_rows_per_region <- data.frame(table(map_regions_fortified$id))
map_regions_fortified <- data.frame(map_regions_fortified,
Num.factories = factor(rep(c(1,0,1,3,1),times=num_rows_per_region$Freq)))
#First, plot without any location dots.
ggplot()+geom_polygon(data=map_regions_fortified,
aes(x = long,y = lat, group=group, fill=Num.factories),colour="black")
Now, let's add the factory locations.
#Set latitude and longitude based on the number of factories per region.
factory_locations <- data.frame(long = c(-65,-55,-51,-44,-42,-38),
lat = c(-5,-15,-27,-7,-12,-8))
#Add a dummy variable, which then allows the colour of the dots
#to be a part of the legend.
factory_locations <- data.frame(factory_locations,
Dummy.var = rep("One dot = one factory location",times=nrow(factory_locations)))
#Replot adding factory location dots.
#We will use black dots here since will be easier to see.
ggplot()+geom_polygon(data=map_regions_fortified,
aes(x = long,y = lat, group=group, fill=Num.factories),colour="black")
+ geom_point(data = factory_locations,aes(x = long,y = lat,colour = Dummy.var))
+ scale_colour_manual(values="black") + labs(colour="")
#Bonus: Let's change the color vector to something more color-blind friendly.
mycol <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442",
"#0072B2", "#D55E00", "#CC79A7","#490092")
ggplot()+geom_polygon(data=map_regions_fortified,
aes(x = long,y = lat, group=group, fill=Num.factories),colour="black")
+ geom_point(data = factory_locations,aes(x = long,y = lat,colour = Dummy.var))
+ scale_colour_manual(values="black") + labs(colour="")
+ scale_fill_manual(values=mycol)

ggplot2: Combine shapefiles from two different geodatasets

I am trying to plot some geolocational data pertaining to Great Britain and Ireland in ggplot. Running the following code, I can successfully map some values from this tab-separated file onto the GBR shapefile data found here (country = Great Britain):
library(rgdal)
library(ggplot2)
library(rgeos)
library(plyr)
#this data comes from http://www.gadm.org/country (download the Great Britain data set, and set path to the downloaded data's topmost directory)
shape.dir <- "C:\\Users\\Douglas\\Desktop\\estc_clean_analysis\\geoanalysis\\GBR_adm"
#the first parameter we pass to readOGR species the location of the shapefile we want to read in; layer indicates which shapefile in that dir we want to read in. Data via UK shapefile from http://www.gadm.org/country
uk.shp <- readOGR(shape.dir, layer = "GBR_adm2")
#read in csv with values by county
small_geo_data <- read.csv(file = "small_geo_sample.txt", header=TRUE, sep="\t", na.string=0, strip.white=TRUE)
#fortify prepares the data for ggplot
uk.df <- fortify(uk.shp, region = "ID_2") # convert to data frame for ggplot
#now combine the values by id values in both dataframes
combined.df <- join(small_geo_data, uk.df, by="id")
#now build plot up layer by layer
ggp <- ggplot(data=combined.df, aes(x=long, y=lat, group=group))
ggp <- ggp + geom_polygon(aes(fill=value)) # draw polygons
ggp <- ggp + geom_path(color="grey", linestyle=2) # draw boundaries
ggp <- ggp + coord_equal()
ggp <- ggp + scale_fill_gradient(low = "#ffffcc", high = "#ff4444",
space = "Lab", na.value = "grey50",
guide = "colourbar")
ggp <- ggp + labs(title="Plotting Values in Great Britain")
# render the map
print(ggp)
Running that code yields:
What I would like to do now is to add data pertaining to Ireland to my plot. I downloaded the "IRL" shapefiles from the same site that provided the GBR shapefiles, but then I ran into a series of roadblocks. I have tried combining IRL_adm1.csv and GBR_adm2.csv (renaming the id values in the former to avoid conflicts), but nothing has worked yet. Before hacking the rest of the way to a kludgy solution, I thought I should stop and post the following question on SO: Is there a reasonably straightforward way to combine the GBR and IRL files in a single plot? I would be very grateful for any ideas or suggestions others can offer on this question.
If your Britain and Ireland shapefiles use the same projection/CRS, you can add both layers to a plot without needing to join them like this:
ggplot() +
geom_polygon(data = gbrshapefortified, aes(long, lat, group = group)) +
geom_polygon(data = irlshapefortified, aes(long, lat, group = group)) +
coord_equal()
I.e. you don't need to combine them if you're just plotting layers and the thematic values you're plotting don't depend on each other.

Resources