0

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)

Image

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?).

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • 1
    Welcome to SO. I assume that your shapefile is something you cannot show in your question. But, you want to provide your code in your question, at least. In SO, you want to provide minimal data and your code when you ask question. This allows SO users to look into your case right away. Otherwise, you would have less chance to receive helps. – jazzurro May 02 '18 at 02:27
  • Hi @jazzurro, thanks for your reply. So, I already shared the script and the shapefile I made in the post. Is the link not working or is it not allowed to use external links in SO? – Danilo Carvalho May 02 '18 at 14:20
  • I think you want to provide your code in your question. At the moment, you have a graphic and text. This is not a good format. As for your data, if it is not possible to upload it in your question, you can provide a link. But you may not receive helps right away since many SO users tend not to download files from external links. I also encourage you to provide files that are ready. For instance, you have a rar file here. This means you ask SO users to unpack it. You may want to provide .shp file instead. Then, it is much straightforward to read the file. – jazzurro May 04 '18 at 03:56
  • I got time to look into your files. As far as I can see, there are 150 grids. I assume that you want to have values for all of them. However, you have 148 data points in the excel file. Why is this the case? – jazzurro May 05 '18 at 13:46
  • @jazzurro Oh, I appreciate your tips, and the two missing grid was just my error. – Danilo Carvalho May 06 '18 at 18:54

2 Answers2

1

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: Result of code

SRVFan
  • 334
  • 1
  • 6
  • 16
  • Hi @SRVFan. So, in my case, I tried `ggplot(data=br.df, aes(x=long, y=lat, group=group)) + geom_polygon(aes(fill=value)) + geom_path(color="grey", linestyle=1) + coord_equal() + scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", na.value = "grey50", guide = "colourbar")` (but separately adding geom_path and geom_polygon), maybe I should have to decide which one I have to keep? Thanks for the help. – Danilo Carvalho May 02 '18 at 14:14
  • I tried `ggplot(teste1.df)+geom_polygon(aes(x=long,y=lat,group=id,fill=value),color="white")+coord_fixed(1.3)+guides(fill=FALSE,color=FALSE)` but these "triangles" still showed up. – Danilo Carvalho May 02 '18 at 20:15
0

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")

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76