2017-08-22 164 views
0

我想創建R中類似於這樣一個情節: enter image description herer多個y軸的互動情節

多達6個變量,它應是有反應。

我試圖積攢,然而在情節上我會得到軸蜱的情節,它變得凌亂,我設法得到只有一個y軸出。

enter image description here

有沒有辦法重新創建plotly情節或任何其他互動庫?

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

回答

4

隨着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

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

謝謝,但問題是,當我有超過4個變量它不會工作,我需要達到6 –