這應該讓你開始。它不會在圓圈內繪製圓圈。 ggplot可以將不同的變量映射到相同的審美(大小),但很難。這裏,點的大小代表總數,點的顏色代表患病的數量。您將需要調整全套數據的大小比例。
下面的代碼獲取城市的地理位置,然後將它們合併回數據文件。然後彙總數據以給出包含所需計數的數據幀。地圖繪製的邊界由城市的最大值和最小值的lon和lat設置。最後一步是繪製地圖上的城市和計數。
# load libraries
library(ggplot2)
library(maps)
library(ggmap)
library(grid)
library(plyr)
# Your data
df <- read.table(header = TRUE, text = "
subjectid location disease
12 'Atlanta, GA' yes
15 'Boston, MA' no
13 'True Blue, Grenada' yes
85 'True Blue, Grenada' yes
46 'Atlanta, GA' yes
569 'Boston, MA' yes
825 'True Blue, Grenada' yes
685 'Atlanta, GA' no
54 'True Blue, Grenada' no
214 'Atlanta, GA' no
685 'Boston, MA' no
125 'True Blue, Grenada' yes
569 'Boston, MA' no", stringsAsFactors = FALSE)
# Get geographic locations and merge them into the data file
geoloc <- geocode(unique(df$location))
pos <- data.frame(location = unique(df$location), geoloc, stringsAsFactors = FALSE)
df <- merge(df, pos, by = "location", all = TRUE)
# Summarise the data file
df = ddply(df, .(location, lon, lat), summarise,
countDisease = sum(ifelse(disease == "yes", 1, 0)),
countTotal = length(location))
# Plot the map
mp1 <- fortify(map(fill = TRUE, plot = FALSE))
xmin <- min(df$lon) - 5
xmax <- max(df$lon) + 7
ymin <- min(df$lat) - 5
ymax <- max(df$lat) + 5
Amap <- ggplot() +
geom_polygon(aes(x = long, y = lat, group = group), data = mp1, fill = "grey", colour = "grey") +
coord_cartesian(xlim = c(xmin, xmax), ylim = c(ymin, ymax)) +
theme_bw()
# Plot the cities and counts
Amap <- Amap + geom_point(data = df, aes(x = lon, y = lat, size = countTotal, colour = countDisease)) +
geom_text(data = df, aes(x = lon, y = lat, label = gsub(",.*$", "", location)), size = 2.5, hjust = -.3) +
scale_size(range = c(3, 10)) +
scale_colour_continuous(low = "blue", high = "red", space = "Lab")
![enter image description here](https://i.stack.imgur.com/yHIn0.png)