![](/img/trans.png)
[英]In Highchart in R how to change colors by negative and positive values in a bar chart
[英]In highchart in R how to create this line + bar chart
我需要使用 highchart R package 復制此圖表:
我認為答案在這里:系列:
[{
type: 'line',
id: 'aapl-ohlc',
name: 'AAPL Stock Price',
data: ohlc
}, {
type: 'column',
id: 'aapl-volume',
name: 'AAPL Volume',
data: volume,
yAxis: 1
}]
看起來我需要使用帶有 2 個系列的hc_add_series
function,對吧?
但這對我來說並不容易。
library(highcharter)
library(gapminder)
gapminder %>% filter(country == c("Chile","Argentina")) %>%
hchart("line",
hcaes(x = year, y = pop, group = country))
有很多方法可以組合線和列系列'。 但是,要使用范圍選擇器,我認為這僅限於圖表類型“股票”。 我整理了幾個例子。 其中第一個向您展示了如何添加范圍選擇器。 其他人則向您展示了一個完全不同的系列。
我修改了數據以添加日期字段。 該字段僅在第一個示例中用於范圍選擇器。 您可以只使用“年份”字段,但它會將 x 軸上的標簽更改為無意義的。 這也可以改變。 我只是選擇了我認為更容易的方法。
對於此圖,請注意我有兩個系列調用和兩個 y 軸調用。 在 y 軸調用中,您會發現relative
。 relative
表示相對於您提供的其他尺寸的尺寸。 如果您對highcharter
有任何了解,我相信 rest 應該是不言自明的。
library(highcharter)
library(tidyverse)
library(gapminder)
data(gapminder)
gp <- gapminder %>% filter(country %in% c("Chile", "Argentina")) %>%
mutate(dt = as.Date(paste0(year, "-01-01"),
format = "%Y-%m-%d", origin = "GMT"))
highchart(type = "stock") %>% hc_rangeSelector(verticalAlign = "bottom") %>%
hc_xAxis(type = "datetime") %>%
hc_add_series(gp, "line", hcaes(x = dt, y = pop, group = country), yAxis = 0) %>%
hc_add_yAxis(title = list(text = "Population"), relative = 2) %>%
hc_add_series(gp, "column",
hcaes(x = dt, y = gdpPercap), yAxis = 1, showInLegend = F) %>%
hc_add_yAxis(title = list(text = "GDP per capita"), relative = 1L)
這里有一些其他用於安排軸的選項。
hchart(gp, "line", hcaes(x = year, y = pop, group = country), yAxis = 0) %>%
hc_yAxis(title = list(text = "Population"), relative = 3) %>%
hc_add_series(gp, "column", hcaes(x = year, y = gdpPercap),
yAxis = 1, showInLegend = F) %>%
hc_add_yAxis(title = list(text = "GDP"), relative = 1)
highchart() %>%
hc_yAxis_multiples(create_axis(2, height = c(4, 1), turnopposite = T)) %>%
hc_add_series(gp, "line", hcaes(x = year, y = pop, group = country), yAxis = 0) %>%
hc_add_series(gp, "column", hcaes(x = year, y = gdpPercap,
group = country), yAxis = 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.