每个州都有基于位置的响应时间数据。我希望能够创建一个地图类型的热图:
有我的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 :(得分: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)
要获取整个地图,只需将其余状态附加到df