在 r 中绘制 vline graph highcharts
plot vline graph highcharts in r
我需要在图表上绘制一条垂直线,并使用包和 highcharts 编写文本“PRE”和“POS”,如附图所示。到目前为止,我还没有成功。到目前为止遵循我的代码。
hc <- bd
plotline <- list(color ="red", value =2015.5, width =5)
hchart('line', hcaes(x = hc$ANO, y = hc$Percentual, group = Niveis)%>%
hc_yAxis(plotLines = list(plotline))
)
hc
垂直线基于 x 轴,因此您需要 hc_xAxis
函数。文本框是用 hc_annotations
.
做的注释
数据
这是图表的示例数据集。
df <- data.frame(year = rep(2012:2020, each = 4),
percent = sample(1:100, 36, replace = T),
group = rep(c("REDE", "GERADOR", "NAO", "PLACA"), 9)
)
代码
library(highcharter)
df %>%
hchart("line",
hcaes(x = year, y = percent, group = group)) %>%
hc_xAxis(title = list(text = "Ano"),
plotLines = list(
list(color ="grey", value = 2015.5, width =5))) %>%
hc_yAxis(title = list(text = "Percentual"),
max = 100,
tickInterval = 25) %>%
hc_annotations(list(labels = list(list(
point = list(x = 2012, y = 95, xAxis = 0, yAxis = 0),
text = "PRE",
backgroundColor = "white",
align = "center",
style = list(fontSize = 15)
),
list(
point = list(x = 2020, y = 95, xAxis = 0, yAxis = 0),
text = "POS",
backgroundColor = "white",
align = "right",
style = list(fontSize = 15)
))))
情节
你没有提供数据,所以我抓了一些来用。我没有尝试匹配样式或颜色 - 你没有表示需要。
library(tidyverse)
library(highcharter)
library(lubridate)
weather # from highcharter package
w2 = weather %>% pivot_longer(min_temperaturec:mean_temperaturec, names_to = "Niveis", values_to = "Percentual")
hchart(w2, 'line', hcaes(x = date,
y = Percentual,
group = Niveis)) %>%
hc_xAxis(plotLines = list(list(value = datetime_to_timestamp(median(w2$date)), # I used the median date
# any 'date' you use needs the function
# datetime_to_timestamp()
width = 5, color = "red"))) %>%
# add Z index to put on top zIndex = 5
hc_annotations(list(
labels =
list(
list(
point = list(x = datetime_to_timestamp(min(w2$date)), # using the minimum date
y = max(w2$Percentual), xAxis = 0, yAxis = 0), # using the maximum y value
text = "PRE"
),
list(
point = list(x = datetime_to_timestamp(max(w2$date)), # using the maximum date
y = max(w2$Percentual), xAxis = 0, yAxis = 0), # using the maximum y value
text = "POST"
))))
我需要在图表上绘制一条垂直线,并使用包和 highcharts 编写文本“PRE”和“POS”,如附图所示。到目前为止,我还没有成功。到目前为止遵循我的代码。
hc <- bd
plotline <- list(color ="red", value =2015.5, width =5)
hchart('line', hcaes(x = hc$ANO, y = hc$Percentual, group = Niveis)%>%
hc_yAxis(plotLines = list(plotline))
)
hc
垂直线基于 x 轴,因此您需要 hc_xAxis
函数。文本框是用 hc_annotations
.
数据 这是图表的示例数据集。
df <- data.frame(year = rep(2012:2020, each = 4),
percent = sample(1:100, 36, replace = T),
group = rep(c("REDE", "GERADOR", "NAO", "PLACA"), 9)
)
代码
library(highcharter)
df %>%
hchart("line",
hcaes(x = year, y = percent, group = group)) %>%
hc_xAxis(title = list(text = "Ano"),
plotLines = list(
list(color ="grey", value = 2015.5, width =5))) %>%
hc_yAxis(title = list(text = "Percentual"),
max = 100,
tickInterval = 25) %>%
hc_annotations(list(labels = list(list(
point = list(x = 2012, y = 95, xAxis = 0, yAxis = 0),
text = "PRE",
backgroundColor = "white",
align = "center",
style = list(fontSize = 15)
),
list(
point = list(x = 2020, y = 95, xAxis = 0, yAxis = 0),
text = "POS",
backgroundColor = "white",
align = "right",
style = list(fontSize = 15)
))))
情节
你没有提供数据,所以我抓了一些来用。我没有尝试匹配样式或颜色 - 你没有表示需要。
library(tidyverse)
library(highcharter)
library(lubridate)
weather # from highcharter package
w2 = weather %>% pivot_longer(min_temperaturec:mean_temperaturec, names_to = "Niveis", values_to = "Percentual")
hchart(w2, 'line', hcaes(x = date,
y = Percentual,
group = Niveis)) %>%
hc_xAxis(plotLines = list(list(value = datetime_to_timestamp(median(w2$date)), # I used the median date
# any 'date' you use needs the function
# datetime_to_timestamp()
width = 5, color = "red"))) %>%
# add Z index to put on top zIndex = 5
hc_annotations(list(
labels =
list(
list(
point = list(x = datetime_to_timestamp(min(w2$date)), # using the minimum date
y = max(w2$Percentual), xAxis = 0, yAxis = 0), # using the maximum y value
text = "PRE"
),
list(
point = list(x = datetime_to_timestamp(max(w2$date)), # using the maximum date
y = max(w2$Percentual), xAxis = 0, yAxis = 0), # using the maximum y value
text = "POST"
))))