我想使用热图包和R中的开源gapminder数据集创建一个分组标签,但是,我很难用分组标签创建一个轴。下面是从highcharter文档创建热图的一些代码:
nyears <- 5
df <- expand.grid(seq(12) - 1, seq(nyears) - 1)
df$value <- abs(seq(nrow(df)) + 10 * rnorm(nrow(df))) + 10
df$value <- round(df$value, 2)
ds <- list_parse2(df)
hc <- highchart() %>%
hc_chart(type = "heatmap") %>%
hc_title(text = "Simulated values by years and months") %>%
hc_xAxis(categories = month.abb) %>%
hc_yAxis(categories = 2016 - nyears + seq(nyears)) %>%
hc_add_series(name = "value", data = ds)
hc_colorAxis(hc, minColor = "#FFFFFF", maxColor = "#434348")现在,假设我有以下数据:
for (package in c('tidyverse', 'gapminder')) {
if (!require(package, character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
data(gapminder)
gapminder <- select(gapminder, continent, country, year, gdpPercap)以下是我的尝试:
gapminder <- select(gapminder, continent, country, year, gdpPercap)
gs <- list_parse2(gapminder)
categories_grouped <- gapminder %>%
group_by(name = continent) %>%
do(categories = array(.$country)) %>%
list_parse()
highchart() %>%
hc_chart(type = "heatmap") %>%
hc_xAxis(categories = categories_grouped) %>%
hc_yAxis(categories = gapminder$year) %>%
hc_add_series(name = 'gdpPercap', data = gs)知道我哪里出问题了吗?
发布于 2020-03-21 00:18:07
我想你是从这个例子开始你的代码的。关于categories_grouped列表的两件事,
所以首先我随机选择了20个国家,这样我们就可以清楚地看到:
library(gapminder)
library(highcharter)
library(dplyr)
set.seed(100)
x_country = sample(gapminder$country,20)
dat<- select(gapminder, continent, country, year, gdpPercap) %>%
filter(country %in% x_country)
dat=droplevels(dat)现在,我为分组x轴创建了分组类别:
categories_grouped <- dat %>%
distinct(continent,country) %>%
rename( name =continent) %>%
group_by(name) %>%
do(categories = .$country) %>%
list_parse()现在我想,我很少用列表,所以我按照下面的设置,应该是可以的:
hc <-highchart() %>%
hc_yAxis(categories = dat$year) %>%
hc_xAxis(categories = categories_grouped) %>%
hc_add_series(data = dat,type = 'heatmap',hcaes(x=country,y=factor(year),value=gdpPercap))
hc_colorAxis(hc, minColor = "#FFFFFF", maxColor = "#434348")

https://stackoverflow.com/questions/60780609
复制相似问题