Search code examples
rplotly

Align text and reduce space between text and parentheses in plotly hover info box


I have this data

filtered_data<-structure(list(authorId = c("100508901", "100998451", "10159591", 
"102319456", "102648923", "103349940", "103471842", "103839825", 
"104207674", "10743363"), topicLabel = c("Societal Bias in AI", 
"Societal Bias in AI", "Societal Bias in AI", "Societal Bias in AI", 
"Societal Bias in AI", "Societal Bias in AI", "Societal Bias in AI", 
"Societal Bias in AI", "Societal Bias in AI", "Societal Bias in AI"
), authorTopicPaperCount = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), topicTotalInfluentialCitationCount = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L), topicAvgInfluentialCitationCount = c(0, 
0, 0, 0, 0, 0, 0, 1, 0, 0), topicTotalCitationCount = c(5L, 3L, 
0L, 0L, 0L, 0L, 0L, 6L, 8L, 0L), topicAvgCitationCount = c(5, 
3, 0, 0, 0, 0, 0, 6, 8, 0), marketShare = c(0.000902527075812274, 
0.000902527075812274, 0.000902527075812274, 0.000902527075812274, 
0.000902527075812274, 0.000902527075812274, 0.000902527075812274, 
0.000902527075812274, 0.000902527075812274, 0.000902527075812274
), authorName = c("R. Wolfe", "Martin Ferianc", "Quincy A. Hathaway", 
"Julie Jiang", "Charith S. Peris", "Bhavesh Neekhra", "J. Kshirsagar", 
"F. M. Zanzotto", "Z. Schillaci", "Utkarsh Agarwal"), authorSumOtherPapers = c(0, 
0, 0, 0, 5, 0, 0, 3, 0, 0), plotlyTopicsAndCounts = structure(c("&nbsp;&nbsp; NA :  0", 
"&nbsp;&nbsp; NA :  0", "&nbsp;&nbsp; NA :  0", "&nbsp;&nbsp; NA :  0", 
"&nbsp;&nbsp; Federated LLM Optimization and Privacy :  1<br>&nbsp;&nbsp;&nbsp;&nbsp; Personality Trait Analysis in LLMs :  1<br>&nbsp;&nbsp;&nbsp;&nbsp; Privacy-Preserving Techniques in LLMs :  3", 
"&nbsp;&nbsp; NA :  0", "&nbsp;&nbsp; NA :  0", "&nbsp;&nbsp; Enhancing LLM Reasoning with CoT and Verification Methods :  1<br>&nbsp;&nbsp;&nbsp;&nbsp; Unassigned Papers :  2", 
"&nbsp;&nbsp; NA :  0", "&nbsp;&nbsp; NA :  0"), names = c(NA, 
NA, NA, NA, "Federated LLM Optimization and Privacy, Personality Trait Analysis in LLMs, Privacy-Preserving Techniques in LLMs", 
NA, NA, "Enhancing LLM Reasoning with CoT and Verification Methods, Unassigned Papers", 
NA, NA)), topicSpecializationScore = c("1.0000", "1.0000", "1.0000", 
"1.0000", "0.1667", "1.0000", "1.0000", "0.2500", "1.0000", "1.0000"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"

and I create the plot below:

library(plotly)
# Create the Plotly graph without color based on total papers in other topics
plot <- plot_ly(data = filtered_data[1:10,], x = ~jitter(marketShare), y = ~topicAvgInfluentialCitationCount, 
                type = 'scatter', mode = 'markers',
                marker = list(size = ~log10(topicTotalCitationCount), sizemode = 'diameter', sizeref = 0.1,
                              color = ~topicSpecializationScore, colorscale = 'Bluered', 
                              colorbar = list(title = "Topic Specialization Score<br>")),
                hoverinfo = 'text',
                text = ~paste('<b>Name:</b>', authorName,
                              '<br><b>Publications (Current Topic):</b>', authorTopicPaperCount,
                              '<br><b>Market Share (Current Topic):</b>', marketShare, 
                              '<br><b>Topic Specialization Score:</b>', topicSpecializationScore,
                              '<br><b>Total Influential Citations (Current Topic):</b>', topicTotalInfluentialCitationCount, 
                              '<br><b>Average Citation Count (Current Topic):</b>', topicAvgCitationCount,
                              '<br><b>Author ID:</b>', authorId,
                              '<br><b>Number of Papers in Other Topics : (',authorSumOtherPapers,'):</b><br>',plotlyTopicsAndCounts))

# Customize layout with wrapped titles
plot <- plot %>% layout(
  xaxis = list(title = 'Market Share of Papers'),
  yaxis = list(title = 'Average Influential Citation Count'),
  margin = list(l = 70, r = 50, b = 90, t = 80),
  hoverlabel = list(align = "left")
)
# Render the plot
plot

As you can see in the image the first name of the list "Enhancing LLM...." is a little to the left from the rest of the names so I would like to align it with the rest of the names, also "( 3 )" should be "(3)" with not so much space between number and parentheses.

enter image description here


Solution

  • To remove the extra whitespace you could remove the &nbsp; from your string and use str_squish to remove the unnecessary whitespaces. If you want to add some space you could add that to the text argument like this:

    library(plotly)
    library(stringr)
    
    filtered_data$plotlyTopicsAndCounts <- gsub("&nbsp;", "", filtered_data$plotlyTopicsAndCounts)
    # Create the Plotly graph without color based on total papers in other topics
    plot <- plot_ly(data = filtered_data[1:10,], x = ~jitter(marketShare), y = ~topicAvgInfluentialCitationCount, 
                    type = 'scatter', mode = 'markers',
                    marker = list(size = ~log10(topicTotalCitationCount), sizemode = 'diameter', sizeref = 0.1,
                                  color = ~topicSpecializationScore, colorscale = 'Bluered', 
                                  colorbar = list(title = "Topic Specialization Score<br>")),
                    hoverinfo = 'text',
                    text = ~paste('<b>Name:</b>', authorName,
                                  '<br><b>Publications (Current Topic):</b>', authorTopicPaperCount,
                                  '<br><b>Market Share (Current Topic):</b>', marketShare, 
                                  '<br><b>Topic Specialization Score:</b>', topicSpecializationScore,
                                  '<br><b>Total Influential Citations (Current Topic):</b>', topicTotalInfluentialCitationCount, 
                                  '<br><b>Average Citation Count (Current Topic):</b>', topicAvgCitationCount,
                                  '<br><b>Author ID:</b>', authorId,
                                  '<br><b>Number of Papers in Other Topics: (',authorSumOtherPapers,')</b><br>',str_squish(plotlyTopicsAndCounts)))
    
    # Customize layout with wrapped titles
    plot <- plot %>% layout(
      xaxis = list(title = 'Market Share of Papers'),
      yaxis = list(title = 'Average Influential Citation Count'),
      margin = list(l = 70, r = 50, b = 90, t = 80),
      hoverlabel = list(align = "left")
    )
    plot
    

    enter image description here

    Created on 2024-01-11 with reprex v2.0.2