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

