R多个y轴交互图 [英] R multiple y axis interactive plot

查看:143
本文介绍了R多个y轴交互图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中创建一个类似于以下内容的图:

I am trying to create a plot in R similar to this:

最多包含6个变量,并且必须是反应性的.

with up to 6 variables and it has to be reactive.

我尝试了图,但是在图上我会在图上得到坐标轴刻度,并且它变得凌乱,并且我设法只输出了一个y轴.

I tried plotly, however in plotly I would get axis ticks on the plot, and it gets messy, and I managed to get only one y axis out.

是否可以在plotly或任何其他交互式库中重新创建图?

Is there a way to recreate the plot in plotly or any other interactive library?

我的情节代码:

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轴的漂亮时间序列图:

With the highcharter package it is possible to create nice time series plots with multiple y-axes:

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

这篇关于R多个y轴交互图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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