将geom_point添加到ggmap(ggplot2) [英] Adding geom_point to ggmap (ggplot2)

查看:202
本文介绍了将geom_point添加到ggmap(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设下面的最小数据集。

  ddf < - 结构(列表(国家= c(阿富汗,阿尔巴尼亚,阿尔及利亚,安哥拉, 阿根廷, 亚美尼亚),
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))

我借用了这篇文章< a>来生成初始地图。

  library(RColorBrewer)
库(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)#我们不需要南极洲
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的大小设置为等于my x2 变量。我不太确定这是如何完成的。我的思想一直以这篇文章为指导,但到目前为止没有运气。任何援助将不胜感激!

解决方案

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

  ddf < - 结构(列表(国家= 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)#我们不需要南极洲

#选择国家
SelectedCountries = subset(wrld,id %c(阿富汗,阿尔巴尼亚,阿尔及利亚,安哥拉,阿根廷,亚美尼亚))

#获得他们的中心
CountryCenters<聚合(cbind(long,lat)〜id,data = SelectedCountries,
FUN = func (x)平均值(范围(x)))

#与DDf数据帧合并
ddf =合并(ddf,CountryCenters,by.x =Country,by.y =id)

gg < - ggplot()+
geom_map(data = wrld,map = wrld,aes(map_id = id,x = long,y = lat),填充=白色,颜色=#7f7f7f,大小= 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),color =red)#绘制点

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

  ddf < - 结构(列表(国家= c(阿富汗,阿尔巴尼亚,阿尔及利亚,安哥拉, 阿根廷,亚美尼亚),
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)#我们不需要Antarctica

#选择国家
SelectedCountries = subset(wrld,id%in %c(阿富汗,阿尔巴尼亚,阿尔及利亚,安哥拉,阿根廷,亚美尼亚))


#计算功能te centroids
GetCentroidPoint< - function(SelectedCountries){Polygon(SelectedCountries [c('long','lat')])@ labpt}

#将函数应用到选定的国家
centroids = by(subset(SelectedCountries,piece == 1),factor(subset(SelectedCountries,piece == 1)$ id),GetCentroidPoint)

#将列表转换为数据框
质心< - do.call(rbind.data.frame,质心)
名称(质心)< -c('long','lat')
质心$国家= row.names(centroids)

#与DDF合并
ddf =合并(ddf,centroids,by ='Country')


gg< ; - ggplot()+
geom_map(data = wrld,map = wrld,aes(map_id = id,x = long,y = lat),fill =white,color =#7f7f7f 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),color =red)


Assume the following minimal data set.

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"))

I borrowed code from this post to generate the initial map. I am assigning color to the countries based on the x1 variable as follows:

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) 

I want to add geom_point to each one of these countries, and set the size of the geom equal to my x2 variable. I'm not quite sure how this is done. My thinking has been guided by this post, but so far no luck. Any assistance would be greatly appreciated!

解决方案

You need a set of coordinates - the centres or centroids for each country. wrld contains long and lat for the boundaries (polygons) for each country. A simple method for calculating centroids is to take the mean of the range of the long and lat values for each country.

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

For better centring, you can use the Polygon function from the sp package, as demonstrated here: stackoverflow.com/.../improve-centering-county-names-ggplot-maps. It gets Argentina wrong, but I think it is something to do with islands. In wrld, I think piece==1 selects the mainland, but it might not work for all countries.

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")

这篇关于将geom_point添加到ggmap(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆