如何使用 R 在 Plotly 中向轴添加子类别?

How can I add a subcategory to axis in Plotly with R?

我正在尝试使用 R 下的 Plotly 向 x 轴添加第二个类别,如下所示:

这是我的代码:

library(plotly)

data <- data.frame(day=  c(1:4),
                   visit = c("visit1","visit1", "visit2", "visit2"),
                   val = c(1:4),
                   flag = c("","","", 1))

fig <- plot_ly(data= data, x = ~day) %>%
  
  add_trace(y = ~val,
            type = 'scatter',
            mode = 'lines+markers',
            line = list(width = 2, 
                        dash = 'solid')) %>%
  
  add_trace(data= data %>% filter(flag == 1), y = 0, 
            type = 'scatter', 
            hoverinfo = "text",
            mode = 'markers',
            name = "flag",
            text = paste("<br>N°",data$ID[data$flag == 1], 
                         "<br>Day",data$day[data$flag == 1]),
            marker = list(
              color = 'red',
              symbol = "x",
              size = 12
            ),
            showlegend = T
  )

fig

我试过了,这看起来不错,但标记从图表中消失了,可能是由于对数据的过滤。

library(plotly)

data <- data.frame(day=  c(1:4),
                   visit = c("visit1","visit1", "visit2", "visit2"),
                   val = c(1:4),
                   flag = c("","","", 1))

fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
  
  add_trace(y = ~val,
            type = 'scatter',
            mode = 'lines+markers',
            line = list(width = 2, 
                        dash = 'solid')) %>%
  
  add_trace(data= data %>% filter(flag == 1), y = 0, 
            type = 'scatter', 
            hoverinfo = "text",
            mode = 'markers',
            name = "flag",
            text = paste("<br>N°",data$ID[data$flag == 1], 
                         "<br>Day",data$day[data$flag == 1]),
            marker = list(
              color = 'red',
              symbol = "x",
              size = 12
            ),
            showlegend = T
  )

fig

你没有提供可重现的问题,所以我做了数据。 (好吧,我无耻地从 here 窃取了数据)。这将创建一个带有两组 x-axis 标签的条形图。每组杆一个。每组酒吧一个。 x-axis 的内容在两个跟踪中是相同的。

library(plotly)

fig <- plot_ly() %>% 
  add_bars(x = list(rep(c("first", "second"), each = 2),
                    rep(LETTERS[1:2], 2)),
           y = c(2, 5, 2, 6),
           name = "Adults") %>% 
  add_bars(x = list(rep(c("first", "second"), each = 2),
                    rep(LETTERS[1:2], 2)),
           y = c(1, 4, 3, 6),
           name = "Children")
fig



更新

您添加了数据和代码,试图将其应用于您的数据。我添加了一个更新,显然错过了问题所在。抱歉。

现在我正在关注(让我们希望,对吧?),这是对实际问题的实际修复。

为了这次改动,我修改了你的数据。我没有将 flag 注释为 1,而是将其更改为零。然后我使用 flag 作为变量。

data <- data.frame(day = c(1:4),
                   visit = c("visit1","visit1", "visit2", "visit2"),
                   val = c(1:4),
                   flag = c("","","", 0))

fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
  add_trace(y = ~val,
            type = 'scatter', mode = 'lines+markers',
            line = list(width = 2, 
                        dash = 'solid')) %>%
  add_trace(y = ~flag, 
            type = 'scatter', hoverinfo = "text",
            mode = 'markers', name = "flag",
            text = paste("<br>N°",data$ID[data$flag == 1], 
                         "<br>Day",data$day[data$flag == 1]),
            marker = list(
              color = 'red', symbol = "x", size = 12),
            showlegend = T)
fig

您将收到有关离散数据和 non-discrete 数据的警告,这不是很准确,但它还是会显示出来。