我有下面的資料框:
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"))
我創建了這個圖,在其中我修改了為黑點顯示的文本。我還想在name點的懸停文本內顯示變數,但它不包含在我用于x和y軸的變數中。
# 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
uj5u.com熱心網友回復:
當您對懸停文本進行細微更改時,我已經展示了一種替換現有內容的方法。然而,為此——添加一個新變數,最容易回到ggplot物件。
順便說一句,我不得不在這段代碼的幾個地方更改
scope_ntis_ciu為。Scope
回傳要更改的圖層:
在geom_jitter圖層中,添加一個呼叫text,aes其中包含您希望在懸停文本中顯示的內容。
你有沒有得到一個錯誤——“找不到name”?如果這樣做,則添加text了aes.
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)
當您為
ggplot物件執行此代碼時,系統會警告ggplot您忽略您的文本——這沒關系,因為它仍然保留資訊并將其直接發送到plotly將使用它的地方。
現在,當您呼叫ggplotly物件時,您可以添加tooltip.
ggplotly(p, tooltip = "text")


轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/437584.html
