我試圖在R中使用highcharts wrapper highcharter
來創建一系列地圖。將狀態或國家顏色繪製爲連續變量的地圖可以很好地工作,但是,我在將狀態顏色繪製爲連續變量時遇到了一些麻煩。 (基本上,我想它看起來像this)。具有R中的分類變量的Highcharter地圖
我試過了我能想到的一切,似乎沒有任何工作。這是一個虛擬數據的例子。假設我想將A類中的狀態顯示爲紅色,將B類顯示爲黃色,將C類顯示爲藍色。
library("dplyr")
library('highcharter')
library("viridisLite")
data(usgeojson)
## Create data frame with letter categories, numerical categories, and state abbreviations
categories <- c("A", "A", "A", "A", "A", "A", "C", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "C", "A", "A", "A", "B",
"B", "B", "B", "B", "C", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "C", "B", "B", "B", "B")
states <- c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI",
"ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS",
"MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR",
"PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")
numbers <- c("1", "1", "1", "1", "1", "1", "3", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "3", "1", "1", "1", "2", "2",
"2", "2", "2", "3", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "3", "2", "2", "2", "2")
data <- data.frame(categories, states, numbers)
## Convert abbreviations to state names for highcharter
data$state_full <- state.name[match(data$state, state.abb)]
## If we plot these data using the numerical categories, the colors are on a scale
highchart(type = "map") %>%
hc_add_series_map(map = usgeojson,
df = data,
joinBy = c("woename", "state_full"),
value = "numbers")
## Plotting by adding each category individually ends up with the each new map
## overwriting the ones before it.
cat_A <- data[data$categories == "A", ]
cat_B <- data[data$categories == "B", ]
cat_C <- data[data$categories == "C", ]
highchart(type = "map") %>%
hc_add_series_map(map = usgeojson,
df = cat_A,
joinBy = c("woename", "state_full"),
value = "numbers") %>%
hc_add_series_map(map = usgeojson,
df = cat_B,
joinBy = c("woename", "state_full"),
value = "numbers") %>%
hc_add_series_map(map = usgeojson,
df = cat_C,
joinBy = c("woename", "state_full"),
value = "numbers")
這顯然可以在highchart中工作,但我似乎無法得到它在highcharter工作。
任何輸入,非常感謝。
謝謝!
有趣 - 無關,但是當我運行這個,現在我得到錯誤'對象「usgeojson」不found'。測試我的其他highcharter地圖類型的項目,他們也失敗了。啊,我胖了。現在加載地圖。但是,他們是單色的。 – wergeld
哦!對於那個很抱歉!我編輯了腳本並添加了必要的'data(usgeojson)'調用。 –