Search code examples
rplotlycategoriesaxis

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


I am trying to add a second category to x axis with Plotly under R like this:

enter image description here

Here is my code:

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

Without Category

I have tried this, which seems good but the markers disappear from the graph, maybe due to the filter on data.

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

Category without filter


Solution

  • You didn't provide a reproducible question, so I've made data. (Well, data I shamelessly stole from here). This creates a bar graph with two sets of x-axis labels. One for each set of bars. One for each group of bars. The content of the x-axis is the same in both traces.

    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
    

    enter image description here



    Update

    You added data and code trying to apply this to your data. I added an update and apparently missed what the problem was. Sorry about that.

    Now that I'm paying attention (let's hope, right?), here is an actual fix to the actual problem.

    For this change, I modified your data. Instead of the flag annotated with a one, I changed it to zero. Then I used flag as a variable.

    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
    

    You're going to get a warning about discrete & non-discrete data, which isn't really accurate, but it shows up, nonetheless.

    enter image description here