2016-08-11 1 views
3

Rマップライブラリを使用して、特定の国の特定の色をプロットしようとしています。私は色を塗りつぶすことができますが、彼らはそれぞれの国に正しく関連付けられていません。私は誰かが手掛かりを持つことができるのだろうかと思います。Rマップライブラリで正しい色をプロットする方法

マイデータフレームは«filldの»で、3列があります。最初は、国の名前で、第二は、単にいくつかの数値データであり、そして第三は色である:これは、コードの私がある

   countries toplot  color 
1    Argentina  -1  red 
2    Armenia  -1  red 
3    Australia  -1  red 
4    Bahrain  -1  red 
5    Botswana  -1  red 
6    Belgium  -1  red 
7    Bulgaria  -1  red 
8    Canada  -1  red 
9     Chile  -1  red 
10    Taiwan  -1  red 
11    Croatia  -1  red 
12  Czech Republic  -1  red 
13  UK:Great Britain  -1  red 
14    Egypt  -1  red 
15    Denmark  -1  red 
16    Finland  0 yellow 
17    France  0 yellow 
18    Georgia  0 yellow 
19    Germany  0 yellow 
20  China:Hong Kong  0 yellow 
21    Hungary  0 yellow 
22   Indonesia  0 yellow 
23     Iran  0 yellow 
24    Ireland  0 yellow 
25    Israel  0 yellow 
26    Italy  0 yellow 
27    Japan  0 yellow 
28    Jordan  0 yellow 
29   Kazakhstan  1 darkgreen 
30    Korea  1 darkgreen 
31    Kuwait  1 darkgreen 
32    Lebanon  1 darkgreen 
33   Lithuania  1 darkgreen 
34    Malaysia  1 darkgreen 
35    Malta  1 darkgreen 
36    Morocco  1 darkgreen 
37   Netherlands  1 darkgreen 
38   New Zealand  1 darkgreen 
39 UK:Northern Ireland  1 darkgreen 
40    Norway  1 darkgreen 
41     Oman  1 darkgreen 
42   Palestine  1 darkgreen 
43    Poland  1 darkgreen 
44    Portugal  1 darkgreen 
45    Qatar  1 darkgreen 
46    Russia  1 darkgreen 
47   Saudi Arabia  0 yellow 
48    Serbia  0 yellow 
49   Singapore  0 yellow 
50  Slovak Republic  0 yellow 
51    Slovenia  -1  red 
52   South Africa  -1  red 
53    Spain  -1  red 
54    Sweden  -1  red 
55    Thailand  1 darkgreen 
56    Turkey  1 darkgreen 
57 United Arab Emirates  0 yellow 
58     USA  1 darkgreen 

使用しています:

library(maps)  # Provides functions that let us plot the maps 
library(mapdata) # Contains the hi-resolution points that mark out the countries. 

map('world', filld$countries, fill=T, border="darkgray", col=filld$color) 
map('world', col="darkgray", add=T) 

しかし、これは私が取得しています色です: Worldmap with wrong colors オーストラリアは赤で満たされる必要がありますが、緑です。スペインは赤く塗るべきですが、黄色です。フランスは黄色く塗りつぶされているが暗緑色でなければならないなど... 米国は暗緑色でなければならない。

コメントをいただければ幸いです。ありがとう!

+0

ggplot2を見て:: geom_map –

+0

おかげリチャード・テルフォード。私はggplotを使って色をプロットすることができます:しかし、今質問はどのように色で凡例を追加するのですか?... "平均以下 - 赤"、 "平均 - Yelow"、 "平均 - 緑"私は伝説を使用することはできません... – JPMD

答えて

4

私はこの問題をどのようにして生成するのかは完全にはわかりませんが、まず世界をプロットしてから、色で塗りつぶすことがそのトリックです。

library(maps) 
library(ggplot2) 

map <- map_data("world") 
map <- subset(map, region!="Antarctica") 
map <- spTransform(map, CRS("+proj=robin")) #Not working, don't know why... 
TimssCountries<-ggplot() + 
    geom_polygon(data = map, aes(x=long, y = lat, group = group), fill = NA, colour="darkgray", size=0.25)+ 
    geom_map(data=filld,map=map,aes(map_id=country, x=lon, y=lat), fill = "filld$color", colour = "gray") + 
    coord_equal() 
TimssCountries 

Map colors

しかし、私はggplotで色の凡例を追加する方法がわからないので、同様のを持っている:@Richardの提案に続き

map('world', col='darkgray') 
for (color in unique(filld$color)) { 
    map('world', regions=filld$countries[which(filld$color==color)], fill=T, border="darkgray", col=color,add=T) 
} 

enter image description here

0

この他の地図への効果:

enter image description here

ありがとう!...

1

元の問題の原因は、

map('world', filld$countries, fill=T, border="darkgray", col=filld$color) 

は色ベクトルとまったく同じ長さのポリゴンの集合を返さないということです。ある国がいくつかのポリゴン(島など)で構成されている場合、これらはすべて別々です。日本では、データに表示される1つの例を示すために、34個のポリゴンから構成されています。

z <- map('world',region='japan') 
z$names 

色が正しく整列しなくなりました。

オプションexact=TRUEを追加するだけで、それぞれの国の主要なポリゴンだけが色付けされます(名前に正確に合うもの)。それはすべての国でも定義されていません。

sel_c <- match.map("world",filld$countries) 
map('world',col=filld$col[sel_c],border="darkgrey",fill=TRUE) 
0

だけrefereceのために誰かを探している場合:「マップ」パッケージとchoroplethsについては

は、あなたの最善の解決策は、選択された領域(すべてのポリゴン)に連番を与えるmatch.map()を、使用することです同様の解決策:scale_fill_identityは、正しい順序で色をプロットします。完全なコードは次のとおりです。

TimssDif<-TimssCountries + 
    geom_map(data = data, map = map, aes(map_id = country, fill = color), colour="darkgray") + 
    theme(legend.title = element_blank()) + # omit plot title saying 'color' 
    scale_fill_identity("Title legend", labels = c("Below mean", "At mean", "Above mean"), breaks = plotclr, guide = "legend") 
TimssDif + theme(legend.position = "bottom") 

enter image description here

+1

米国は暗緑色であるはずですか?また、いくつかの国が欠けている。 – jgadoury

+0

残念ながら、最初の投稿でテーブルを使用していません...概念の証明だけです – JPMD

+0

また、map_data関数はリージョンとサブリージョンを分割します。それがあなたが英国を見ない理由です。英国を追加するには:英国は、アレックスDeckmyn提案に続いて、それらを再び連結しなければなりません: ' map $ region [map($ subregion ==" Great Britain ")] ="イギリス:イギリス " map $ region [地図($ subregion == "北アイルランド")] = "イギリス:北アイルランド" map $ region [地図($ subregion == "香港")] = "中国:香港" – JPMD

関連する問題