Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

r - Missing data when Supplying a Dual-axis--Multiple-traces to subplot

Data is provided at the bottom of the question.

I am trying to use subplot with plotly objects which one of them has multiple series in it. When I use subplot one of the series in the first graph does not show up in the final product. Look at the code below:

library(plotly)
library(dplyr)

sec_y <- list(tickfont = list(color = "red"),
              overlaying = "y",
              side = "right",
              title = "Lft")


pp1 <- fmean1 %>% group_by(grp) %>%  plot_ly() %>%
  add_lines(x = ~hour, y = ~fmgd, name = "FMGD", colour = "blue") %>%
  add_lines(x = ~hour, y = ~lft, name = "Lft", yaxis = "y2", colour = "red") %>%
     layout(title = "Fbay", yaxis2 = sec_y,
            xaxis = list(title="Date"))



pp2 <- prcpf1 %>% plot_ly() %>%
  add_bars(x=~Datetime, y=~precip, name = "prcp")



subplot(pp1, pp2, nrows = 2 , shareX = T)

This is what I got from subplot with only FMGD plotted:

enter image description here

While the top plot should be like below:

enter image description here

Question: Is this a bug in subplot or am I missing something?

Data:

fmean1 <- structure(list(hour = structure(1:11, .Label = c("2018-04-15 
18:00:00",  "2018-04-15 19:00:00", "2018-04-15 20:00:00", "2018-04-15 21:00:00", 
"2018-04-15 22:00:00", "2018-04-15 23:00:00", "2018-04-16 00:00:00", 
"2018-04-16 01:00:00", "2018-04-16 02:00:00", "2018-04-16 03:00:00", 
"2018-04-16 04:00:00"), class = "factor"), fmgd = c(249.67262, 
278.45789, 349.241726666667, 351.883898333333, 369.18035, 406.85811, 
406.233883333333, 393.751951666667, 390.004548333333, 403.980246666667, 
449.06727), lft = c(84.7313333333333, 85.1555, 85.8243333333333, 
87.7796666666667, 88.8493333333333, 88.1606666666667, 87.1883333333333, 
86.2645, 85.9258333333333, 86.3718333333333, 86.4433333333333
), grp = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), 
row.names = 91:101, class = "data.frame")

.

prcpf1 <- structure(list(Datetime = structure(c(1523815200, 1523818800, 
1523822400, 1523826000, 1523829600, 1523833200, 1523836800, 1523840400, 
1523844000, 1523847600, 1523851200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), precip = c(0.11, 0.09, 0.06, 0.09, 0.03, 0.04, 
0.02, 0.14, 0.07, 0.15, 0.26)), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame"))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looking at this issue on github This turned out to be a in .

Referencing to the link above, what we need to do is explicitly defining the y axes for all the plots, all the axes. Look below for its implementation on my example:

## Creating axis layouts, explicitly for all y axes
L_Axis <- list(tickfont = list(color = "red"), overlaying = "y",
               side = "right", title = "Lft")

F_Axis <- list(side = "left", title = "fmgd")

P_Axis <- list(side = "left", title = "prcp")


pp1 <- fmean1 %>% group_by(grp) %>%  plot_ly() %>%
  add_lines(x = ~hour, y = ~fmgd, name = "FMGD", colour = "blue") %>%
  add_lines(x = ~hour, y = ~lft, name = "Lft", yaxis = "y2", colour = "red") %>%
  layout( yaxis  = F_Axis, #left axis
          yaxis2 = L_Axis, #right axis
          title = "Fbay", xaxis = list(title="Date"))



pp2 <- prcpf1 %>% plot_ly() %>%
  add_bars(x=~Datetime, y=~precip, name = "prcp", yaxis = "y", colour = "green") %>% 
  layout(yaxis = P_Axis) #only y axis, on the left



pp <- subplot(pp1, pp2 , nrows = 2 , titleY = T, shareX = T)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...