Home > Software design >  Add more than two variables in hover text of plotly object with more than one traces
Add more than two variables in hover text of plotly object with more than one traces

Time:03-04

I have the dataframe below:

ct<-structure(list(name = c("Afghanistan India", "Afghanistan India", 
"Albania Kosovo", "Albania Kosovo", "Bangkok Agreement", "Bangkok Agreement", 
"Bangkok Agreement", "Bangkok Agreement", "Bangkok Agreement", 
"Belarus Russia (Union State)", "Belarus Russia (Union State)", 
"Albania Macedonia", "Albania Macedonia", "Belarus Serbia", "Belarus Serbia", 
"Belarus Ukraine", "Belarus Ukraine", "Belize Guatemala", "Belize Guatemala", 
"Bhutan India"), Country = c("Afghanistan", "India", "Albania", 
"Kosovo", "Bangladesh", "India", "Laos", "South Korea", "Sri Lanka", 
"Belarus", "Russia", "Albania", "North Macedonia", "Belarus", 
"Serbia", "Belarus", "Ukraine", "Belize", "Guatemala", "Bhutan"
), Scope = c(3, 3, 23, 23, 23, 23, 23, 23, 23, 26, 26, 6, 6, 
6, 6, 6, 6, 1, 1, 5), year2 = c(2000, 2000, 2000, 2000, 1975, 
1975, 1975, 1975, 1975, 1995, 1995, 2000, 2000, 2005, 2005, 1990, 
1990, 2005, 2005, 2005), pta_count = c(2, 3, 8, 1, 1, 1, 1, 1, 
1, 2, 2, 8, 8, 1, 4, 2, 7, 2, 3, 1)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

and I create this plot in which I modified the text displayed for the black dots.I want also to dislay the variable name inside the hover text of dots but it is not included in variables which I use for x and y axis.

# for instance
i <- 2

p<-ct %>% filter(Country==unique(ct$Country)[i]) %>%
  ggplot(aes(year2,Scope)) geom_jitter() 
  geom_col(aes(y=pta_count/(max(dt2$pta_count)/max(dt2$scope_ntis_ciu))),
           fill="darkolivegreen",alpha=0.3,width=3) 
  xlim(c(1950,2020)) 
  scale_y_continuous(
    limits=c(0,33),
    # Features of the first axis
    name = "NTI Scope\n(scope measures the sum of all NTIs mentioned in a PTA,\ndot indicated one PTA)",
    
    # Add a second axis and specify its features
    sec.axis = sec_axis( ~ . * max(dt2$pta_count)/max(dt2$scope_ntis_ciu), name="PTA Count\n(green columns indicate number of PTAs\n signed in given 5-year intervall)")
  ) 
  labs(x='',title=unique(ct$Country)[i],
       subtitle = paste0('signed ',sum(ct[ct$Country=="India",]$pta_count),' PTAs in total and\nhas an average ',mean(ct[ct$Country==unique(ct$Country)[i],]$Scope),' NTI-scope index across all years'))

# create plotly object to modify
p2 <- plotly_build(p)

# now modify the text calls for each trace that this applies to

# modfiy existing tooltips
# this is the first trace (the bar chart or first geom in ggplot object)
p2$x$data[[1]]$text <- str_replace_all(p2$x$data[[1]]$text, 
                                       "year2", "Year ") %>% 
  str_replace_all(., fixed("Scope"),
                  "Count of issues ")
p2

CodePudding user response:

When you made the minor changes to the hover text, I had shown a method to replace what was there. However, for this–the addition of a new variable, it's easiest to go back to the ggplot object.

BTW I had to change scope_ntis_ciu to Scope in a few places in this code.

Return to the layers that you want to change:

In the geom_jitter layer, add a call for text in aes with what you want to appear in your hover text.

Did you get an error–'can't find name'? If you do, you added the text outside of aes.

geom_jitter(aes(text = paste0("Name: ", name, "\nYear: ", year2,
                              "\nCount of issues: ", Scope)))  

Then your column layer:

geom_col(aes(y=pta_count/(max(pta_count)/max(Scope)),
             text = paste0("Year: ", year2, "\nCount of issues: ",
                           pta_count/(max(pta_count)/max(Scope)))),
         fill="darkolivegreen",alpha=0.3,width=3) 

When you execute this code for the ggplot object, you will be warned that ggplot is ignoring your text–that's okay, because it still keeps the information and sends it right along to plotly, where it will be used.

Now when you call the ggplotly object, you can add the tooltip.

ggplotly(p, tooltip = "text")

enter image description here

enter image description here

  • Related