我试图绘制世界地图并根据值填写。当我运行以下代码时,它似乎打破了地图。由于缺乏更好的术语,地图看起来很奇怪。
我错过了什么?
CODE& DATA
url='https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv'
df = read.csv(url)
library(ggmap)
library(parallel)
# Get Lon and Lat for each country
countries = as.character(df$country)
df$region = df$country
coords = mclapply(X = countries,FUN= function(x) geocode(x))
coords=do.call(rbind.data.frame,coords)
library(RColorBrewer)
library(maptools)
library(ggplot2)
library(rworldmap)
map.world = map_data(map='world')
map.world = merge(df,map.world, by='region',all.y=TRUE )
gg = ggplot()
gg = gg + geom_map(data = map.world,map = map.world,
aes(map_id = region,x=long,y=lat,fill=map.world$beer_servings))
gg= gg+ scale_fill_gradient(breaks=c(500000,1000000,1500000),
labels=c("Low","Medium","High"))
gg = gg + coord_equal()
ggsave(filename = 'myplot.png',plot = gg)
答案 0 :(得分:1)
我必须在合并数据后重置订单:
url='https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv'
df = read.csv(url)
df$region = as.character(df$country)
library(RColorBrewer)
library(maptools)
library(ggplot2)
library(rworldmap)
map.world = map_data(map='world')
map.world = merge(df,map.world, by='region',all.y=TRUE)
map.world = map.world[order(map.world$order), ] # <---
ggplot() +
geom_map(data = map.world,map = map.world, aes(
map_id = region,
x=long,
y=lat,
fill=beer_servings
)) +
coord_quickmap()