Masoud
Masoud

Reputation: 595

R plotly frame with multiple x-axis

I am trying to have a line plot using plotly as follows:

library(plotly)
library(dplyr)
datatoplot<-structure(list(year = c(1397, 1397, 1397, 1397, 1397, 1397, 1397, 
                                1397, 1397, 1397, 1397, 1397, 1398, 1398, 1398, 1398, 1398, 1398, 
                                1398, 1398, 1398, 1398, 1398, 1398, 1399, 1399, 1399, 1399, 1399, 
                                1399, 1399, 1399, 1399, 1399, 1399, 1399), month = c(1, 2, 3, 
                                                                                     4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
                                                                                     11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), v10 = c(2.8, 
                                                                                                                                             5.1, 6.3, 4.8, 4.1, 2.9, 3, 3.6, 4.4, 4.1, 3.4, 5.9, 1.7, 2.5, 
                                                                                                                                             2.1, 2.7, 2.3, 3, 3.2, 3.6, 2, 2, 1.6, 2.6, 1.5, 4, 2.9, 3.6, 
                                                                                                                                             3.6, 2.5, 2.2, 1.6, 1.9, 2.2, 3.6, 4.5), v14 = c(0.67, 0.65, 
                                                                                                                                                                                              0.56, 0.65, 0.73, 0.67, 0.6, 0.64, 0.61, 0.61, 0.69, 0.64, 0.69, 
                                                                                                                                                                                              0.75, 0.76, 0.73, 0.76, 0.68, 0.73, 0.92, 0.69, 0.66, 0.78, 0.71, 
                                                                                                                                                                                              0.67, 0.68, 0.69, 0.73, 0.68, 0.76, 0.73, 0.81, 0.75, 0.8, 0.8, 
                                                                                                                                                                                              0.71)), row.names = c(NA, 36L), class = "data.frame")

datatoplot %>% plot_ly( alpha  = 0.5) %>%
  add_lines(
    x = ~list(year,month), y = ~v10
    #,frame=~list(year,month)
    ,line = list(simplify = FALSE)
  ) %>%
  layout(yaxis = list(title = "A"))

The output is as follows:

enter image description here

Now I want to add a frame to the plot using 'frame' parameters by

frame=~list(year,month)

But unfortunately it does not work and the output is as follows:

enter image description here How to fix it?

Upvotes: 0

Views: 159

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33397

Not sure if this is your expected output but in my eyes it would make sense to set year as the frame:

library(plotly)
library(dplyr)
datatoplot <- structure(
    list(
      year = c(1397, 1397, 1397, 1397, 1397, 1397, 1397, 1397, 1397, 1397,
      1397, 1397, 1398, 1398, 1398, 1398, 1398, 1398, 1398, 1398, 1398, 1398,
      1398, 1398, 1399, 1399, 1399, 1399, 1399, 1399, 1399, 1399, 1399, 1399,
      1399, 1399),
      month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8,
      9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
      v10 = c(2.8, 5.1, 6.3, 4.8, 4.1, 2.9, 3, 3.6, 4.4, 4.1, 3.4, 5.9, 1.7,
      2.5, 2.1, 2.7, 2.3, 3, 3.2, 3.6, 2, 2, 1.6, 2.6, 1.5, 4, 2.9, 3.6, 3.6,
      2.5, 2.2, 1.6, 1.9, 2.2, 3.6, 4.5),
      v14 = c(0.67, 0.65, 0.56, 0.65, 0.73, 0.67, 0.6, 0.64, 0.61, 0.61, 0.69,
      0.64, 0.69, 0.75, 0.76, 0.73, 0.76, 0.68, 0.73, 0.92, 0.69, 0.66, 0.78,
      0.71, 0.67, 0.68, 0.69, 0.73, 0.68, 0.76, 0.73, 0.81, 0.75, 0.8, 0.8,
      0.71)
    ),
    row.names = c(NA, 36L),
    class = "data.frame"
  )

datatoplot %>% plot_ly(alpha  = 0.5) %>%
  add_lines(
    x = ~ month,
    y = ~ v10,
    frame = ~ year,
    line = list(simplify = FALSE)
  ) %>%
  layout(yaxis = list(title = "A"))

result

Upvotes: 1

Related Questions