2013-05-31 70 views
1

我有基於位置的響應時間數據,每個州。我想能夠創建熱圖的地圖類型:如何在ggplot中創建基於地圖的熱圖

有我的DF:

structure(list(DATE_TIME = structure(c(1369419660, 1369419720, 
1369419720, 1369419780, 1369419780, 1369419840, 1369419840, 1369419900, 
1369419960, 1369419960, 1369419960, 1369420020, 1369420020, 1369420020, 
1369420020, 1369420080, 1369420080, 1369420080, 1369420080, 1369420140, 
1369420140, 1369420140, 1369420140, 1369420200, 1369420200, 1369420260, 
1369420260, 1369420260, 1369420260, 1369420260), class = c("POSIXct", 
"POSIXt"), tzone = ""), SITE = c("Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts", "Logon to My Accounts", "Logon to My Accounts", 
"Logon to My Accounts"), RESPONSE_TIME = c(7.069, 7.056, 11.535, 
7.33, 9.566, 5.21, 6.483, 6.652, 8.222, 9.368, 10.055, 6.301, 
6.33, 7.802, 10.132, 6.241, 6.997, 7.499, 7.823, 6.173, 6.912, 
7.979, 10.128, 7.072, 7.65, 6.048, 7.681, 8.08, 8.272, 9.583), 
    AVAIL_PERCENT = c(100L, 100L, 100L, 100L, 100L, 100L, 100L, 
    100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
    100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
    100L, 100L, 100L), AGENT = c(45869L, 45540L, 45672L, 45036L, 
    45421L, 42627L, 44981L, 42432L, 45869L, 45693L, 42108L, 40522L, 
    40521L, 45540L, 45672L, 40517L, 45036L, 45421L, 40511L, 42627L, 
    44981L, 40370L, 40369L, 40368L, 42432L, 40282L, 45693L, 42108L, 
    40296L, 45869L), LOCATION = c("seattle", "hartford", "houston", 
    "san diego", "montreal", "new york", "philadelphia", "chicago", 
    "seattle", "dallas", "pittsburgh", "miami", "denver", "hartford", 
    "houston", "atlanta", "san diego", "montreal", "milwaukee", 
    "new york", "philadelphia", "vancouver", "toronto", "calgary", 
    "chicago", "san jose", "dallas", "pittsburgh", "mexico city", 
    "seattle")), .Names = c("DATE_TIME", "SITE", "RESPONSE_TIME", 
"AVAIL_PERCENT", "AGENT", "LOCATION"), row.names = c(NA, 30L), class = "data.frame") 

我嘗試這樣做:

require(maps) 
require(ggplot2) 

ggplot(df, aes(map_id = LOCATION)) + geom_map(aes(fill = RESPONSE_TIME), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat) 

任何想法,我做錯了什麼?

+0

'states_map'對象來自哪裏? – harkmug

+0

@rmk我試圖做這個states_map <-map_data(「狀態」) – user1471980

+0

@rmk我不需要子區域。 – user1471980

回答

0

df$LOCATION轉換爲對應的狀態。

負載數據集:

data(us.cities) 
data(state, package="datasets") 

c2s = sapply(df$LOCATION,function(x){ 
      us.cities[grep(x,us.cities$name,ignore.case=T)[1],2]}) 

> head(c2s) 
    seattle hartford houston san diego montreal new york 
    "WA"  "CT"  "TX"  "CA"  NA  "NY" 

獲取狀態的縮寫:

a2n = tolower(state.name) 
names(a2n) = state.abb 
df = cbind(df,a2n[c2s]) 

> head(df) 
      DATE_TIME     SITE RESPONSE_TIME AVAIL_PERCENT AGENT 
1 2013-05-24 14:21:00 Logon to My Accounts   7.069   100 45869 
2 2013-05-24 14:22:00 Logon to My Accounts   7.056   100 45540 
3 2013-05-24 14:22:00 Logon to My Accounts  11.535   100 45672 
4 2013-05-24 14:23:00 Logon to My Accounts   7.330   100 45036 
5 2013-05-24 14:23:00 Logon to My Accounts   9.566   100 45421 
6 2013-05-24 14:24:00 Logon to My Accounts   5.210   100 42627 
    LOCATION c2s a2n[c2s] 
1 seattle WA washington 
2 hartford CT connecticut 
3 houston TX  texas 
4 san diego CA california 
5 montreal <NA>  <NA> 
6 new york NY new york 

colnames(df)[7:8] = c("State.Abb","State") 

漏下加拿大國家和情節:

ggplot(df[!is.na(df$State),], aes(map_id = State)) + geom_map(aes(fill = RESPONSE_TIME), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat) 

enter image description here

ŧ o得到整個地圖,只是將其餘的狀態附加到底部df

+0

我需要查看whoe地圖,所有州和hightlight我有數據的州。 – user1471980

+0

將城市映射到狀態的一種方法是使用'data(us.cities)'查找匹配項:'us.cities [grep(df $ LOCATION [1],us.cities $ name,ignore.case = T), ]' – harkmug