开发者

Fill continental areas in geographical maps created with R

开发者 https://www.devze.com 2023-04-09 13:48 出处:网络
I try to get into creating geographical maps using the maps-package in R. I\'m not sure if this is the \"package of choice\".

I try to get into creating geographical maps using the maps-package in R. I'm not sure if this is the "package of choice".

I tried to draw a map of the world with a bluish background, white continental areas and red country borders. But it seems, that it is not possible to fill a开发者_如何学JAVAreas without drawing country borders in black. What is my mistake?

library(maps)

# Show the World map without country border,
# with black continent boundaries on a bluish background 
map("world", interior=F, boundary=T, bg="#ddeeff", col="black")

# Fill continental areas in white
map("world", col="white", interior=F, fill=TRUE, add=T)

# Add country borders in red
map("world", interior=T, boundary=F, col="red", add=T)

Using ?map gives me:

fill - logical flag that says whether to draw lines or fill areas. If FALSE, the lines bounding each region will be drawn (but only once, for interior lines). If TRUE, each region will be filled using colors from the col = argument, and bounding lines will not be drawn.

My R-Version is:

platform x86_64-apple-darwin9.8.0

arch x86_64

os darwin9.8.0

system x86_64, darwin9.8.0

status

major 2

minor 13.0

year 2011

month 04

day 13

svn rev 55427

language R

version.string R version 2.13.0 (2011-04-13)


Using base graphics:

(There is a certain amount of overplotting of red on top of black boundaries - I couldn't find a way of getting rid of this altogether.)

library(maps)
map("world", interior=TRUE, fill=TRUE, boundary=FALSE, col="white", bg="lightblue")
map("world", interior=TRUE, boundary=TRUE, col="red", add=TRUE)

Fill continental areas in geographical maps created with R

Using ggplot:

(With ggplot you have full control over all the colours and there is no overplotting. But it takes a bit longer to render...)

library(ggplot2)
library(maps)
mapWorld <- borders("world", colour="red", fill="white")
ggplot() + 
  mapWorld + 
  geom_path() + 
  opts(
    plot.background=theme_rect(fill="lightblue"),
    panel.background=theme_rect(fill="lightblue")
    )

Fill continental areas in geographical maps created with R


It is possible to change the color of the borders in a single 'map' command:

map("world", fill=TRUE, col="white", bg="lightblue", border="red")

The "border='red'" option is not documented explicitely in '?map', because it is in fact an option directly for "polygon()" which is called from inside map(). It's part of the "..." in the map() call.


Using base graphics:

In order to avoid the overplotting, you can set the lty argument to 0 in the first call to fill the countries without drawing the black boundaries.

library(maps)   
map("world", interior=TRUE, fill=TRUE, boundary=FALSE, col="white", lty=0, bg="lightblue")
map("world", interior=TRUE, boundary=TRUE, col="red", add=TRUE)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号