gpt4 book ai didi

r - R 中带有分类变量的 Highcharter map

转载 作者:行者123 更新时间:2023-12-04 16:11:11 27 4
gpt4 key购买 nike

我正在尝试使用 R 中的 highcharts 包装器 highcharter 来创建一系列 map 。将州或国家/地区颜色绘制为连续变量的 map 效果很好,但是,我在将州颜色绘制为连续变量时遇到了一些问题。 (基本上,我希望它看起来像 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 中工作。

非常感谢任何输入。

谢谢!

最佳答案

第三个系列中没有数据的状态显示为灰色状态,并覆盖了之前系列中的数据。你可以设置 allAreasfalse 以防止这种情况。

此外,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")

关于r - R 中带有分类变量的 Highcharter map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com