在具有多个轨迹的 plotly 对象的悬停文本中添加两个以上的变量

Add more than two variables in hover text of plotly object with more than one traces

我有以下数据框:

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"))

我创建了这个图,其中我修改了为黑色显示的文本 dots.I 还想在点的悬停文本中显示变量 name 但它不包含在变量中我用于 xy 轴。

# 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

当您对悬停文本进行细微更改时,我展示了一种替换现有内容的方法。不过,对于这个——新变量的添加,最简单的还是回到ggplot对象

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

Return 到您要更改的图层:

geom_jitter 层中,在 aes 中添加对 text 的调用,以及您希望在悬停文本中显示的内容。

您是否收到错误消息 –“找不到 name”?如果这样做,则您在 aes.

之外添加了 text
geom_jitter(aes(text = paste0("Name: ", name, "\nYear: ", year2,
                              "\nCount of issues: ", Scope))) +

然后你的柱层:

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.

现在调用 ggplotly 对象时,可以添加 tooltip.

ggplotly(p, tooltip = "text")