R多y轴交互图

R multiple y axis interactive plot

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

最多 6 个变量,并且必须是反应式的。

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

有没有办法在 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)
  )

我刚刚调整了您的代码。希望这对您有所帮助!

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",
      color="red", 
      title = ""
    ),
    yaxis2 = list(
      overlaying = "y",
      anchor = "free",
      color="blue", 
      title= ""
    ),
    yaxis3 = list(
      side = "right",
      overlaying = "y",
      color="green", 
      title= ""
    ),
    yaxis4 = list(
      side = "right",
      overlaying = "y",
      anchor = "free",
      position = 1,
      color="pink",
      title= ""
    ),
    xaxis = list(
      title = ""
    ),
    margin = list(pad = 25)
  )

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

要添加和修改@Prem 的答案,您可以通过使用 domain 调整图表的 x 轴部分来避免 y 轴与图表本身重叠x轴的参数。

plot_ly(data, x = ~time, type = 'scatter', mode = 'lines') %>%
    add_lines(y = ~y1, name='y1', line = list(color = "red")) %>%
    add_lines(y = ~y2, name='y2', yaxis='y2', line = list(color = "orange")) %>%
    add_lines(y = ~y3, name='y3', yaxis='y3', line = list(color = "darkgreen")) %>%
    add_lines(y = ~y4, name='y4', yaxis='y4', line = list(color = "purple")) %>%
    add_lines(y = ~y5, name='y5', yaxis='y5', line = list(color = "brown")) %>%
    layout(
       xaxis = list(title = "time", domain = c(0.5,1)),
       yaxis = list(title = 'Y-axis 1', side = "left", color = "red", position = 0,
               anchor = 'free'),
       yaxis2 = list(title = 'Y-axis 2', side = "left", color = "orange", 
                overlaying = "y", anchor = 'free', position = 0.1),
       yaxis3 = list(title = 'Y-axis 3', side = "left", 
                overlaying = "y", anchor = 'free', color = "darkgreen", position = 0.2),
       yaxis4 = list(title = 'Y-axis 4', side = "left", 
                overlaying = "y", anchor = 'free', color = "purple", position = 0.3),
       yaxis5 = list(title = 'Y-axis 5',side = "left", 
                overlaying = "y", anchor = 'free', color = "brown", position = 0.4),
       showlegend = T
)

可以使用 position 参数设置单个 y 轴相对于绘图大小的位置(以百分比表示)。 在上面的示例中,每个 y 轴占总绘图大小的 10%,而图形保留给右半部分。