我正在尝试使用R中的highcharts包装器highcharter
来创建一系列地图。将州或国家颜色绘制成连续变量的地图可以很好地工作,但是,我在绘制状态颜色到连续变量时遇到了一些麻烦。 (基本上,我希望它看起来像this)。
我已经尝试了所有我能想到的东西,但似乎没有任何效果。这是虚拟数据的一个例子。假设我想将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")
这显然可以在highcharts中使用,但我似乎无法让它在highcharter中工作。
非常感谢任何输入。
谢谢!
答案 0 :(得分:0)
没有数据的第三个系列的状态显示为灰色状态,并且超过了之前系列的数据。您可以将allAreas
设置为false
以防止此情况发生。
此外,colorAxis需要获得最大设置,因为它仅针对第一个系列自动计算 - the issue reported。
工作代码(在代码之后运行 - 在设置类别后):
highchart(type = "map") %>%
hc_plotOptions(series = list(allAreas = F)) %>%
hc_colorAxis(max = 3) %>%
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")
答案 1 :(得分:0)
您只需要复制所需的示例。
cat_A <- data %>% filter(categories == "A")
cat_B <- data %>% filter(categories == "B")
cat_C <- data %>% filter(categories == "C")
map <- download_map_data("countries/us/us-all")
hc <- highchart(type = "map") %>%
hc_plotOptions(map = list(
allAreas = FALSE,
joinBy = c("hc-a2", "states"),
mapData = map
)) %>%
hc_add_series(name = "A", data = cat_A, color = "#A1A1A1") %>%
hc_add_series(name = "B", data = cat_B, color = "#46BEC8") %>%
hc_add_series(name = "C", data = cat_C, color = "#0000CD")
hc
其他选择:
series <- data %>%
group_by(name = categories) %>%
do(data = list_parse(select(., states))) %>%
ungroup() %>%
mutate(color = c("red", "darkred", "pink"))
series
highchart(type = "map") %>%
hc_plotOptions(map = list(
allAreas = FALSE,
joinBy = c("hc-a2", "states"),
mapData = map
)) %>%
hc_add_series_list(series)