gpt4 book ai didi

R多y轴交互图

转载 作者:行者123 更新时间:2023-12-05 08:55:28 25 4
gpt4 key购买 nike

我正在尝试在 R 中创建一个与此类似的图: enter image description here

最多有 6 个变量,并且必须是响应式(Reactive)的。

我在 plotly 中尝试过,但是在 plotly 中我会在 plotly 上得到轴刻度,它变得困惑,我设法只得到一个 y 轴。

enter image description here

有没有办法在 plotly 或任何其他交互式库中重新创建情节?

我的剧情代码:

library(dplyr)
library(plotly)
library(tidyr)

data <- cbind(
seq(from = 1, to = 30, by = 1),
sample(seq(from = 100, to = 300, by = 10), size = 30, replace = TRUE),
sample(seq(from = 1, to = 100, by = 9), size = 30, replace = TRUE),
sample(seq(from = 50, to = 60, by = 2), size = 30, replace = TRUE),
sample(seq(from = 100, to = 130, by = 1), size = 30, replace = TRUE)
) %>%
as.data.frame()

names(data) <- c("date", "a", "b", "x", "y")

plot_ly(x = ~data$date) %>%
add_lines(y = ~data[, 2], name = "a", line = list(color = "red")) %>%
add_lines(y = ~data[, 3], name = "b", line = list(color = "blue"), yaxis = "y2") %>%
add_lines(y = ~data[, 4], name = "x", line = list(color = "green"), yaxis = "y3") %>%
add_lines(y = ~data[, 5], name = "y", line = list(color = "pink"), yaxis = "y4") %>%
layout(
yaxis = list(
side = "left",
title = list("")
),
yaxis2 = list(
side = "left",
overlaying = "y",
anchor = "free"
),
yaxis3 = list(
side = "left",
overlaying = "y",
anchor = "free",
position = 0.04
),
yaxis4 = list(
side = "left",
overlaying = "y",
anchor = "free",
position = 0.08
),
margin = list(pad = 30)
)

最佳答案

使用 highcharter 包可以生成具有多个 y 轴的漂亮时间序列图:

library(highcharter)

set.seed(1)
n <- 100
x1 <- cumsum(rnorm(n))
x2 <- cumsum(runif(n)-0.5)+10
x3 <- cumsum(rnorm(n,0,20))+100
x4 <- cumsum(rnorm(n,0,20))+1000

highchart() %>%
hc_add_series(data = x1) %>%
hc_add_series(data = x2, yAxis = 1) %>%
hc_add_series(data = x3, yAxis = 2) %>%
hc_add_series(data = x4, yAxis = 3) %>%
hc_yAxis_multiples(
list(lineWidth = 3, lineColor='#7cb5ec', title=list(text="First y-axis")),
list(lineWidth = 3, lineColor="#434348", title=list(text="Second y-axis")),
list(lineWidth = 3, lineColor="#90ed7d", title=list(text="Third y-axis")),
list(lineWidth = 3, lineColor="#f7a35c", title=list(text="Fourth y-axis"))
)

enter image description here

关于R多y轴交互图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45812466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com