添加 geom_point 到 ggmap (ggplot2)
Adding geom_point to ggmap (ggplot2)
假设以下最小数据集。
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 的指导,但到目前为止还没有运气。如有任何帮助,我们将不胜感激!
您需要一组坐标 - 每个国家/地区的中心或质心。 wrld
包含每个国家/地区边界(多边形)的经度和纬度。计算质心的一种简单方法是取每个国家/地区的 long 和 lat 值范围的平均值。
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
为了更好地居中,您可以使用 sp
包中的 Polygon
函数,如下所示:whosebug.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")
假设以下最小数据集。
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 的指导,但到目前为止还没有运气。如有任何帮助,我们将不胜感激!
您需要一组坐标 - 每个国家/地区的中心或质心。 wrld
包含每个国家/地区边界(多边形)的经度和纬度。计算质心的一种简单方法是取每个国家/地区的 long 和 lat 值范围的平均值。
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
为了更好地居中,您可以使用 sp
包中的 Polygon
函数,如下所示:whosebug.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")