Search code examples
rplotly

How to change r and theta labels in R plotly popup


I want to create a radar plot using plotly in R. I create numeric values and short names (columns 'question_short' and 'values') for plotting (not to overload the aesthetics of the graphic, because in the real data the labels are much longers), but for the popup in the plot I would like to show full values (columns 'questions' and 'values_label'), is it possible?

Now it shows values from 'question_short' as theta and 'question_short' as r, that are the ones uset for plotting. See image:

enter image description here

In the popup, Value label: should be 'Low', and the Question: should be 'willing to work'.

This is the example code:


plot_df <- data.frame(

  questions = c('level of expertise?', 'willing to work', 'motivation', 'Its friday', 'Chuck Norris'),
  question_short = c('LOE', 'WTW', 'mtvtn', 'frdy', 'ChuNor'),
  values_label = c("Very Low", "Low", "Medium", "High", "Very High"),
  values = c(0:4)
)
 
fig_topic <- plot_ly(
  type = 'scatterpolar',
  fill = 'toself'
)  %>%
  add_trace(
    r = plot_df$values,
    theta = plot_df$question_short,
    name = plot_df$group_id,
    hovertemplate = paste('<i>Value label</i>: %{r}',
                          '<br><b>Question</b>: %{theta}<br>')
  )
 
fig_topic

Thank you!


Solution

  • You can create your tooltip using the text= attribute which allows to use the "longer" versions of your data columns and setting hoverinfo="text" to dsiplay only the text attribute in the tooltip:

    library(plotly)
    
    fig_topic <- plot_ly(
      plot_df,
      type = "scatterpolar",
      fill = "toself"
    ) %>%
      add_trace(
        r = ~values,
        theta = ~question_short,
        #name = ~group_id,
        text = ~paste0(
          "<i>Value label</i>:", values_label,
          "<br><b>Question</b>: ", questions, "<br>"
        ),
        hoverinfo = "text"
      )
    
    fig_topic
    

    enter image description here