将geom_point添加到ggmap(ggplot2)

时间:2015-03-26 19:17:09

标签: r ggplot2 ggmap

假设以下最小数据集。

ddf <- structure(list(Country = c("Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Armenia"), 
x1 = c(16L, 7L, 2L, 1L, 11L, 4L), x2 = c(1150.36, 9506.12, 7534.06, 6247.28, 18749.34, 6190.75)), 
.Names = c("Country", "x1", "x2"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L), class = "data.frame", 
na.action = structure(2L, .Names = "2", class = "omit"))

我从this post借用代码来生成初始地图。我根据x1变量为国家/地区分配颜色,如下所示:

library(RColorBrewer)
library(maptools)
library(ggplot2)
data(wrld_simpl)
wrld_simpl@data$id <- wrld_simpl@data$NAME
wrld <- fortify(wrld_simpl, region="id")
wrld <- subset(wrld, id != "Antarctica") # we don't rly need Antarctica
gg <- ggplot()
gg <- gg + geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(data=ddf, map=wrld, aes(map_id=Country, fill=x1),  color="white", size=0.25) 

我想在这些国家/地区中添加geom_point,并将geom的大小设置为我的x2变量。我不太清楚这是怎么做到的。我的想法一直由this post引导,但到目前为止还没有运气。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您需要一组坐标 - 每个国家/地区的中心或质心。 wrld包含每个国家/地区的边界(多边形)的长度和纬度。计算质心的一种简单方法是取每个国家的长值和纬度值范围的平均值。

ddf <- structure(list(Country = c("Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Armenia"), 
x1 = c(16L, 7L, 2L, 1L, 11L, 4L), x2 = c(1150.36, 9506.12, 7534.06, 6247.28, 18749.34, 6190.75)), 
.Names = c("Country", "x1", "x2"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L), class = "data.frame", 
na.action = structure(2L, .Names = "2", class = "omit"))

library(RColorBrewer)
library(maptools)
library(ggplot2)

data(wrld_simpl)
wrld_simpl@data$id <- wrld_simpl@data$NAME
wrld <- fortify(wrld_simpl, region="id")
wrld <- subset(wrld, id != "Antarctica") # we don't rly need Antarctica

# Select the countries
SelectedCountries = subset(wrld, id %in% c("Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Armenia"))

# Get their centres
CountryCenters <- aggregate(cbind(long, lat) ~ id, data = SelectedCountries, 
                    FUN = function(x) mean(range(x)))

# Merge with the DDf data frame
ddf = merge(ddf, CountryCenters, by.x = "Country", by.y = "id")

gg <- ggplot() +
   geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25) +
   geom_map(data=ddf, map=wrld, aes(map_id=Country, fill = x1), size=0.25) +
   geom_point(data=ddf, aes(x=long, y=lat, size = x2), colour = "red")     # plot the points

为了更好地居中,您可以使用Polygon包中的sp功能,如下所示:stackoverflow.com/.../improve-centering-county-names-ggplot-maps。这让阿根廷错了,但我认为这与岛屿有关。在wrld中,我认为piece==1会选择大陆,但它可能对所有国家都不起作用。

ddf <- structure(list(Country = c("Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Armenia"), 
x1 = c(16L, 7L, 2L, 1L, 11L, 4L), x2 = c(1150.36, 9506.12, 7534.06, 6247.28, 18749.34, 6190.75)), 
.Names = c("Country", "x1", "x2"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L), class = "data.frame", 
na.action = structure(2L, .Names = "2", class = "omit"))

library(RColorBrewer)
library(maptools)
library(ggplot2)

data(wrld_simpl)
wrld_simpl@data$id <- wrld_simpl@data$NAME
wrld <- fortify(wrld_simpl, region="id")
wrld <- subset(wrld, id != "Antarctica") # we don't rly need Antarctica

# Select the countries
SelectedCountries = subset(wrld, id %in% c("Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Armenia"))


# Function to calculate centroids
GetCentroidPoint <- function(SelectedCountries) {Polygon(SelectedCountries[c('long', 'lat')])@labpt}

# Apply the function to the selected countries
centroids = by(subset(SelectedCountries, piece == 1), factor(subset(SelectedCountries, piece == 1)$id), GetCentroidPoint) 

# Convert list to data frame
centroids <- do.call("rbind.data.frame", centroids)  
names(centroids) <- c('long', 'lat') 
centroids$Country = row.names(centroids)

# Merge with DDF
ddf = merge(ddf, centroids, by = 'Country')


gg <- ggplot() +
   geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25) +
   geom_map(data=ddf, map=wrld, aes(map_id=Country, fill = x1), size=0.25) +
   geom_point(data=ddf, aes(x=long, y=lat, size = x2), colour = "red")

enter image description here