R中高章程热图的分组类别 [英] Grouped categories for a highcharter heatmap in R

查看:145
本文介绍了R中高章程热图的分组类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个热图使用highercharter包和R中的开源gapminder数据集.但是,我很难用分组标签.以下是根据highcharter文档创建热图的一些代码:

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. Here is some code on creating a heat map from the highcharter documentation:

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)

这是我的尝试:

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?

推荐答案

我猜您是从此示例.关于categorys_grouped列表的两件事,

I guess you started your code with this example. Two things about the categories_grouped list,

  1. 您需要一对一的映射,没有多余的内容,如果您看一下示例,它是从不同的类制造商的数据帧开始的.

  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.

列表的子元素必须命名为名称"和类别"

the sub elements of the list has to be named "name" and "categories"

因此,我首先随机选择20个国家/地区进行绘制,以便清晰可见:

So first I select randomly 20 countries to plot, so that it can be seen clearly:

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轴创建分组的类别:

Now I create the categories grouped for the grouped x-axis:

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

这篇关于R中高章程热图的分组类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆