2017-08-22 5 views
0

Je tente de créer un terrain en R semblable à ceci: enter image description hereR multiple axe y Intrigue interactive

avec jusqu'à 6 variables et il doit être réactif.

J'ai essayé l'intrigue, mais dans l'intrigue, j'obtiendrais des graduations d'axe sur l'intrigue, et cela devient salissant, et j'ai réussi à obtenir seulement un axe y.

enter image description here

Est-il possible de recréer l'intrigue dans plotly ou toute autre bibliothèque interactive?

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

Répondre

4

Avec le paquet highcharter il est possible de créer des tracés de séries chronologiques belles avec plusieurs axes 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

Je viens peaufiné votre code. J'espère que cela t'aides!

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

Merci, mais la question est quand j'ai plus de 4 variables ça ne marchera pas, et j'ai besoin d'avoir jusqu'à 6 –