0

I was trying to make a map of Brazil with background color, in other words, I want to give a color to the ocean. I was practicing with the same idea as the post below.

How can I color the ocean blue in a map of the US?

But it doesn't work well with the Brazil map. Does someone know why straight lines are drawn inside the map contour?

library(maps)
BRA <- map(database="world", regions="Brazil", plot=F)
X_border <- BRA$x
Y_border <- BRA$y
xbox <- range(BRA$x, na.rm=T) + c(-2,2)
ybox <- range(BRA$y, na.rm=T) + c(-2,2)
plot(xbox, ybox, type = "n", ann=F)
polypath(X_border, Y_border)

enter image description here

1 Answers1

0

Thank you user51187286016, in fact, some paths or line segments of the Brazilian polygon are in the anti-clockwise direction. I decided to share some code that solved this.

  1. Split the X and Y objects, to segregate the island's polygons [1:254] and then remove all NA's.
library(maps)
BRA <- map(database="world", regions="Brazil", plot=F)
X  <- BRA$x
Y  <- BRA$y
X2 <- na.exclude(X[254:1921])
Y2 <- na.exclude(Y[254:1921])

  1. Set reverse order of two segments (736:823 and 943:1130) and concatenate them with the remaining segments into another object
X3 <- c(X2[1:735], X2[823:736], X2[824:942], 
X2[1130:943], X2[1131:1658]) 
           
Y3 <- c(Y2[1:735], Y2[823:736], Y2[824:942],
Y2[1130:943], Y2[1131:1658])
               

If you want to color the ocean blue in a map, now you can use the function polypath(), as below:

xbox <- range(BRA$x, na.rm=T) + c(-2,2) # Set the box margins
ybox <- range(BRA$y, na.rm=T) + c(-2,2)

plot(xbox, ybox, type = "n", ann=F, xaxs="i", yaxs="i")
polypath(c(X3, NA, X[1:252], NA, xbox, rev(xbox)),  
         c(Y3, NA, Y[1:252], NA, rep(ybox, each=2)), col="royalblue")

For curiosity, I will share a long code that I used to identify the line segments. Maybe there is a shortcut to do the same thing.

lines(X[1:10], Y[1:10], col="blue4", lwd=2)   #island
lines(X[12:19], Y[12:19], col="blue4", lwd=2) #island
lines(X[21:31], Y[21:31], col="blue4", lwd=2) #island
lines(X[33:43], Y[33:43], col="blue4", lwd=2) #island
lines(X[45:53], Y[45:53], col="blue4", lwd=2) #island
lines(X[55:63], Y[55:63], col="blue4", lwd=2) #island
lines(X[65:74], Y[65:74], col="blue4", lwd=2) #island
lines(X[76:84], Y[76:84], col="blue4", lwd=2) #island
lines(X[86:99], Y[86:99], col="blue4", lwd=2) #island
lines(X[86:99], Y[86:99], col="blue4", lwd=2) #island
lines(X[101:175], Y[101:175], col="blue4", lwd=2) #island
lines(X[177:188], Y[177:188], col="blue4", lwd=2) #island
lines(X[190:200], Y[190:200], col="blue4", lwd=2) #island
lines(X[202:214], Y[202:214], col="blue4", lwd=2) #island
lines(X[216:229], Y[216:229], col="blue4", lwd=2) #island
lines(X[216:229], Y[216:229], col="blue4", lwd=2) #island
lines(X[231:241], Y[231:241], col="blue4", lwd=2) #island
lines(X[243:252], Y[243:252], col="blue4", lwd=2) #island
lines(X[254:296], Y[254:296], col="orange")
lines(X[298:355], Y[298:355], col="red")
lines(X[357:919], Y[357:919], col="blue")
lines(X[921:991], Y[921:991], col="red")
lines(X[993:1080], Y[993:1080], col="orange")
lines(X[1082:1200], Y[1082:1200], col="green")
lines(X[1202:1389], Y[1202:1389], col="blue")
lines(X[1391:1545], Y[1391:1545], col="red")
lines(X[1547:1676], Y[1547:1676], col="orange")
lines(X[1678:1799], Y[1678:1799], col="green")
lines(X[1801:1921], Y[1801:1921], col="blue")
ouflak
  • 2,458
  • 10
  • 44
  • 49