简体   繁体   中英

Problems with a Choropleth map

Im trying to obtain a Choropleth map in R using map and ggplot2 packages. Im interesting in just represent specifics countries and specific values for them. My data is the following:

GEO<-c("ES","GB","FR","DE","DK","AT","PT")
Value<-c(0.2560,0.023,0.0120,0.158,0.0025,0.158,0.2)
countries=c("Spain","Great Britain","France","German","Denmark","Austria","UK")
datar<-data.frame(GEO,Value,countries)

datar$GEO corresponds to the ISO 2 code name for countries.

I obtain a Choropleth map using this script:

mapa<-fortify(map(regions=datar$countries,col="grey20",fill=TRUE,plot=FALSE))

gg <- ggplot()
gg <- gg + geom_map(data=mapa, map=mapa,
                aes(x=long, y=lat, map_id=region),
                fill="white", color="black")
gg<- gg +geom_map(data=datar, map=mapa,
              aes(map_id=countries,fill=Value),color="blue",size=0.25)
gg <- gg + coord_map()
gg <- gg + theme_bw()
gg

Here the map: 在此处输入图片说明

First problem is that Grate Britain is not in the picture. I tried to change the nomenclature to "UK". It is worst since it represents Namibia not England.. I try to search for the correct name of UK but didn't find anything! Someone knows? datar$GEO does not produce even a plot [Error en seq_len(nrow(data) - 1) : argument must be coercible to non-negative integer]

Second. I would like to create a color scale in red from 0 to 0.5. Where and how I should do it?

And last, How can I remove the lat and long numbers, along with the background lines and square.

Thanks!

There is in fact a simple fix. data.frame() by default changes a character vector into a factor, which can confuse code in many ways. You probably see warnings about that:

Warning message:
In `[<-.factor`(`*tmp*`, iexp, value = "UK$)|(^UK:") :
  invalid factor level, NA generated

So indeed you need to use "UK" in stead of "Great Britain" or "United Kingdom", but the NAmibia can be avoided by using

datar <- data.frame(GEO,Value,countries, stringsAsFactors=FALSE)

This error with factors only pops up for the UK because of the hackish fix for "uk" vs "ukraine". I'll fix that in the next release (3.2) of maps.

geom_map() is always a bit of pain to deal with, I learned that.

The correct code for Great Britain is indeed UK , but, contrary to the other countries you search for, it has sub-regions. Searching for just UK will return NA and this (weirdly) will return NAmibia .
The solution is to search for UK: .

Btw, you can use map_data() instead of fortify(map(..)) .

library(ggplot2)
library(maps)

datar <- data.frame(GEO = c("ES","UK","FR","DE","DK","AT","PT"), 
                    Value = c(0.2560,0.023,0.0120,0.158,0.0025,0.158,0.2),
                    search_countries = factor(c("Spain","UK:","France","Germany","Denmark","Austria", "Portugal")),
                    countries = c("Spain","UK","France","Germany","Denmark","Austria", "Portugal")
)

mapa <- map_data('world', region = datar$search_countries)

We still need the countries column as they were originally, to join mapa and datar .

ggplot() + 
  geom_map(data = mapa, map = mapa, aes(x = long, y = lat,map_id = region)) +
  geom_map(data = datar, map = mapa, aes(fill = Value, map_id = countries), color ='black', size = .25) +
  coord_map() 
#> Warning: Ignoring unknown aesthetics: x, y

This is where the geom_map weirdness comes out. but you probably already know that, as you correctly plotted two geom_map and the weirdness about the required but warning x & y aesthetics.


Last thing to do is to fix the fill scale, and remove the theme's elements that aren't needed (You could achieve the same result using theme_void() )

last_plot() +
  scale_fill_gradient(low = 'white', high = 'red', limits = c(0, .5)) +
  theme(panel.background = element_rect(colour = 'black', fill = 'white'),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM