2014-03-05 47 views
2

我想創建一個地圖,用圓圈顯示我的數據集中的主題起源於哪個城市。我希望這些圈子與我的數據中城市中的人數成正比。我還想要一個額外的圈子成爲原始圈子的一個子集,顯示每個城市患病的人。ggmap在地圖上用圓圈表示數據

我已經開始用ggmap這樣通過獲取經度和緯度:

library(ggplot2) 
library(maps) 
library(ggmap) 
geocode("True Blue, Grenada") 

我堅持,因爲我不知道該怎麼繼續。我無法單獨加載美國地圖,因爲在加勒比海有一個地點。

這裏是我的短格式數據,實際數據集太大了。

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 

有人能幫忙嗎?

回答

1

這應該讓你開始。它不會在圓圈內繪製圓圈。 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