2013-05-31 50 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