简体   繁体   English

R 中 highcharter 热图的分组类别

[英]Grouped categories for a highcharter heatmap in R

I'd like to create a heat map using the highercharter package and the open source gapminder dataset in R. However, I'm having difficulty creating an axis with grouped labels .我想使用highercharter包和R 中的开源gapminder数据集创建一个热图。但是,我很难创建一个带有分组标签的轴。 Here is some code on creating a heat map from the highcharter documentation:以下是从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")

Now, let's say I have the following data:现在,假设我有以下数据:

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)

And here is my attempt:这是我的尝试:

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)

Any idea of where I'm going wrong?知道我哪里出错了吗?

I guess you started your code with this example .我猜你用 这个例子开始了你的代码。 Two things about the categories_grouped list,关于 category_grouped 列表的两件事,

  1. you need a 1-to-1 mapping, no redundancies, if you looked at the example, it started off from a dataframe of distinct class manufacturer.你需要一个 1 对 1 的映射,没有冗余,如果你看这个例子,它是从不同类制造商的数据帧开始的。

  2. the sub elements of the list has to be named "name" and "categories"列表的子元素必须命名为“名称”和“类别”

So first I select randomly 20 countries to plot, so that it can be seen clearly:所以首先我随机选择了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)

Now I create the categories grouped for the grouped x-axis:现在我为分组的 x 轴创建分组的类别:

categories_grouped <- dat %>%
  distinct(continent,country) %>% 
  rename( name =continent) %>%
  group_by(name) %>%
  do(categories = .$country) %>% 
  list_parse()

Now I plot, I seldom used a list, so I go with the setting below, should be ok:现在我绘图,我很少使用列表,所以我使用下面的设置,应该没问题:

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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM