R:在updatemenus中标出多个参数相同的标签

时间:2019-04-28 10:29:03

标签: r plotly

我对plotly有点陌生,我正在尝试制作一个前后圆点图,您可以在其中通过下拉菜单切换变量。我实际上实现了这一点,但是我想拥有一个颜色图例功能,可以将前后差异的方向分为“ Before> After”,“ Before dir_y 。 updatemenus更新变量(在我的示例中为yz),但我不知道如何更新dir_ydir_z,同时仅保留两个下拉选项(“ Var y ”和“ Var z”)。不用说,我需要更新dir_ydir_z以从图例中仅选择一个类别(“ Before> After”,“ Before y或z取决于从下拉列表中选择的哪个。我在应该进行dir_ydir_z更新的地方添加了2条评论,但没有任何尝试。

谢谢。任何帮助是极大的赞赏。

这是我的代码:

library(plotly)
library(tidyverse)

set.seed(81)
df <- data.frame(id = rep(1:100, 2),
                 x = c(rep("pre", 100), rep("post", 100)), 
                 y = runif(200), 
                 z = rnorm(200, mean = 50, sd = 10))
df <- df[-sample(1:nrow(df), size = 20) , ]                               # delete some rows at random to simulate missing values


df_plotly <- 
  df %>%
  mutate(x = forcats::fct_relevel(x, "pre", "post")) %>%                  # relevel Pre Post for plot
  mutate(jit_x = jitter(as.numeric(x))) %>%                               # add jitter to x discrete var before piping to plotly
  mutate(y = round(y, 2),
         z = round(z, 2)) %>%                                             # round y & z
  group_by(id) %>%                                                        # group by id
  mutate(dif_y = coalesce(lag(y) - y, y - lead(y)),                       # do Pre - Post by id for y
         dif_z = coalesce(lag(z) - z, z - lead(z))) %>%                   # do Pre - Post by id for z
  mutate(dir_y = case_when(dif_y != 0 && dif_y > 0 ~ "Pre > Post",
                           dif_y != 0 && dif_y < 0 ~ "Pre < Post",
                           dif_y == 0              ~ "Pre = Post",
                           TRUE                    ~ "Unpaired"),
         dir_z = case_when(dif_z != 0 && dif_z > 0 ~ "Pre > Post",
                           dif_z != 0 && dif_z < 0 ~ "Pre < Post",
                           dif_z == 0              ~ "Pre = Post",
                           TRUE                    ~ "Unpaired"))



p1 <-
  df_plotly %>%
  plot_ly(x = ~jit_x, y = ~y) %>%  
  add_trace(x = ~jit_x, y = ~y, color = ~dir_y, colors = c("red", "lightgrey", "green", "black"), 
            mode = 'markers+lines', type = 'scatter', hoverinfo = 'text+y',   
            text = ~paste("ID: ", id, "<br>") 
            ) %>% 
  layout(
    title = "",
    xaxis = list(title = "",
                 tickvals = list(1, 2),                       # jitter(1:2) from 2 levels factor produces values around 1 & 2, should be fine
                 ticktext = list("Pre", "Post") ),
    yaxis = list(title = "",
                 hoverformat = '.2f',            
                 zeroline = F),
    updatemenus = list(
      list(
        buttons = list(
          list(method = "restyle",
               args = list("y", list(df_plotly$y)  # , "dir_y", list(df_plotly$dir_y)
                           ),  
               label = "Var Y"),
          list(method = "restyle",
               args = list("y", list(df_plotly$z)  # ,  "dir_y", list(df_plotly$dir_z)
                           ),  
               label = "Var Z")))
    ))
p1

enter image description here

0 个答案:

没有答案