R: график с двойной осью Y? - PullRequest
0 голосов
/ 15 сентября 2018

Я нашел этот вопрос, но ответ не актуален для получения правильного результата.

Вторая ось Y на графике R

Как можно построить график с двойной осью y?

df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), format = "%Y-%m-%d"),
                 Spend = c(39654, 34446, 27402),
                 Visits = c(19970, 14450, 12419))



plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
  add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
  layout(yaxis2 = list(overlaying = "y", side = "right"))

Выдает:

enter image description here

Что мне нужно (но вместо бара и линии, 2 строки):

enter image description here

1 Ответ

0 голосов
/ 15 сентября 2018

Вот способ сделать это:

df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), 
                                     format = "%Y-%m-%d"),
                 Spend = c(39654, 34446, 27402),
                 Visits = c(19970, 14450, 12419))

old.y <- list(
  side = "left",
  title = "Spend"
)

new.y <- list(
  overlaying = "y",
  side = "right",
  title = "Visits"
)

plot_ly(df) %>%
  add_lines(x = ~MediaDate, y = ~Spend, yaxis="y1") %>%
  add_lines(x = ~MediaDate, y = ~Visits, yaxis = "y2") %>%
  layout(yaxis2 = new.y, yaxis = old.y, xaxis = list(title="MediaDate"))

enter image description here

...