R多个y轴交互图

时间:2017-08-22 08:22:16

标签: r plot interactive multiple-axes

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

最多6个变量,必须是被动的。

我尝试了一下,但是在情节上我会在曲线上得到轴刻度,并且它变得混乱,我设法只得到一个y轴。

enter image description here

有没有办法在剧情或任何其他互动图书馆中重建情节?

我的情节代码:

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

3 个答案:

答案 0 :(得分:5)

使用[JsonIgnore] public List<Something> Somethings { get; set; } //Ignore by default public List<Something> Somethings { get; set; } JsonConvert.SerializeObject(myObject, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 包可以创建具有多个y轴的漂亮时间序列图:

highcharter

enter image description here

答案 1 :(得分:1)

我只是调整了你的代码。希望这有帮助!

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

答案 2 :(得分:1)

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

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%,而图形保留给右半部分。