1

州ごとに、ロケーションベースの応答時間データがあります。ヒートマップのマップ タイプを作成できるようにしたいと思います。

私の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)

私がここで間違っていることは何ですか?

4

1 に答える 1

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

于 2013-05-31T20:23:49.603 に答える