1

I'm having trouble displaying text on top of markers using multiple traces on same plotly graphs. Here's the code:

plot_ly(x = ~SWITCHING_SUMS_NET()$week, y = ~SWITCHING_SUMS_NET()$V1, 
        name = 'Affordability', type = 'scatter', mode = 'lines+markers', 
        text=~SWITCHING_SUMS_NET()$V1, textposition="top center") %>%
    add_trace(y = ~SWITCHING_SUMS_NET()$V2, name = 'Experience Change', 
              mode = 'lines+markers', text=~SWITCHING_SUMS_NET()$V2) %>% 
    add_trace(y = ~SWITCHING_SUMS_NET()$V3, name = 'Unavailability', 
              mode = 'lines+markers', text=~SWITCHING_SUMS_NET()$V3) %>% 
    add_trace(y = ~SWITCHING_SUMS_NET()$V4, name = 'Attracted buy another brand', 
              mode = 'lines+markers', text=~SWITCHING_SUMS_NET()$V4) %>%    
    layout(xaxis = list(title = ''),
           yaxis = list (title = 'Count',showticklabels = T), 
           legend = list(orientation = 'h', xanchor = "center",x = 0.5,y= -0.3))

Output image:

Output_image

M--
  • 25,431
  • 8
  • 61
  • 93
Ismail Akrim
  • 13
  • 1
  • 3
  • I was able to address your problem now (at least I hope that I was). But you should always provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It should be [minimal, but complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). The question should be clear and specific. Cheers. – M-- Jun 30 '20 at 01:31

1 Answers1

1

Whenever you have to do multiple add_trace (or other plotly functions) in plotly or geoms in ggplot, investigate possibility of reshaping your dataframe from wide to long. This way you can create a grouping column and do only one add_trace (here is an example for ggplot2: Making multi-line plots in R using ggplot2).

Moreover, it is preferred to read in your data using the reactive function into a variable within shiny (i.e. plot_data <- SWITCHING_SUMS_NET()) outside of your plotting function. This way you can later apply filters, reshaping or other necessary functions on your data and with minimal edits your plot will be produced again. Also, your input data stays the same.

Considering hints above, this is the best way to approach this problem. Here, I have two traces, one for text and the other for markers and lines (within plot_ly function itself). The other approach would be using annotation which I don't see as efficient. Read more here: https://plotly.com/r/text-and-annotations/

## packages
library(shiny)
library(plotly)
library(dplyr)
library(tidyr)

## reading reactive to a variable
plot_data <- SWITCHING_SUMS_NET()

plot_data %>%
   ## rename variables to the names to be used in the plot
   rename_at(c("V1", "V2", "V3", "V4"), 
             list(~c("Affordability", "Experience Change", 
                     "Unavailability", "Attracted buy another brand"))) %>% 
  ## reshape data from wide to long
  pivot_longer(-week) %>% 
 ## plot the rehsaped data 
  ## add a trace for liens and markers with legend
 plot_ly(x = ~week, y = ~value, color = ~name,
         type = 'scatter', mode = 'lines+markers',
         showlegend = T) %>%
  ## add a trace for text without legend
   ## insert `color = I("Black")` to have texts in one for all
  add_text(text=~value, textposition="top center",
            showlegend = F) %>% 
  ## set the layout
  layout(xaxis = list(title = ''),
         yaxis = list (title = 'Count', showticklabels = T), 
         legend = list(orientation = 'h', xanchor = "center", x = 0.5, y = -0.3))
  

Example:

I also added an illustration using iris dataset since you haven't shared your data.

library(dplyr)
library(tidyr)
library(plotly)

iris %>% 
  filter(Species == "setosa") %>% 
  slice(1:10) %>% 
  arrange(Sepal.Length) %>% 
  pivot_longer(-c(Species, Sepal.Length)) %>% 

 plot_ly(x=~Sepal.Length, y=~value, color = ~name,
         type = 'scatter', mode = 'lines+markers', 
         showlegend = T) %>% 
   add_text(text=~value, textposition="top center",
            showlegend = F) %>% 
   layout(xaxis = list(title = ''),
         yaxis = list (title = 'Count', showticklabels = T), 
         legend = list(orientation = 'h', xanchor = "center", x = 0.5, y = -0.3))

M--
  • 25,431
  • 8
  • 61
  • 93