r-如何在使用ggplotly时删除悬停显示中的图例条目



我使用ggplotly创建了以下图形。我正在寻找一个解决方案,我可以删除悬停显示中的图例条目。我只希望悬停显示显示日期(x轴(和值(y轴(。

p1 <- ggplot(sa_inflation, aes(Date,Rate, col=Key)) +
geom_line(size = 1.2) +
theme_bw() +
labs(y='Annual Inflation Rate (%)') +
theme(legend.title = element_blank()) +
scale_colour_manual(values=c('#75002B','#EFBE93')) +
scale_x_date(breaks = seq(head(sa_inflation$Date, 1), 
tail(sa_inflation$Date, 1),
by = '1 year'), date_labels = '%b-%Y')

p1 %>% ggplotly() %>% 
add_annotations(xref='paper',yref='paper',x=1,y=-0.07,text='*Source - FRED',showarrow=F,
font=list(size=10,color='black')) %>% 
add_annotations(xref='paper',yref='paper',x=1,y=1.035,text='*Core Inflation excludes food and energy items in consumer basket',showarrow=F,
font=list(size=10,color='black')) 

这是我的数据集的复制品

structure(list(Date = structure(c(17045, 17075, 17106, 17136, 
17167, 17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 
17440, 17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 
17713, 17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 
17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 
18262, 18293, 18322, 18353, 18383, 18414, 17045, 17075, 17106, 
17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348, 17379, 
17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622, 17652, 
17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 17928, 
17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201, 
18231, 18262, 18293, 18322, 18353, 18383, 18414), class = "Date"), 
Key = c("Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Headline", "Headline", "Headline", "Headline", 
"Headline", "Core", "Core", "Core", "Core", "Core", "Core", 
"Core", "Core", "Core", "Core", "Core", "Core", "Core", "Core", 
"Core", "Core", "Core", "Core", "Core", "Core", "Core", "Core", 
"Core", "Core", "Core", "Core", "Core", "Core", "Core", "Core", 
"Core", "Core", "Core", "Core", "Core", "Core", "Core", "Core", 
"Core", "Core", "Core", "Core", "Core", "Core", "Core", "Core"
), Rate = c(6.466, 6.774, 6.867, 7.066, 6.794, 6.492, 6.127, 
5.247, 5.339, 5.005, 4.357, 4.564, 4.858, 4.632, 4.418, 4.5, 
4.274, 3.835, 3.718, 4.301, 4.288, 4.377, 5.049, 4.85, 4.826, 
5.005, 5.096, 4.402, 3.908, 4.072, 4.528, 4.405, 4.393, 4.473, 
3.974, 4.348, 4.144, 3.666, 3.568, 4.033, 4.404, 4.55, 4.061, 
2.873, 2.059, 2.141, 5.7, 5.846, 5.613, 5.938, 5.398, 5.332, 
4.76, 4.749, 4.666, 4.685, 4.564, 4.431, 4.467, 4.303, 4.267, 
4.072, 4.176, 3.953, 4.062, 4.304, 4.389, 4.031, 4.178, 3.946, 
4.13, 4.161, 4.255, 4.351, 4.291, 4.369, 4.41, 4.114, 4.003, 
4.311, 4.073, 4.365, 4.042, 3.888, 3.891, 3.757, 3.583, 3.767, 
3.566, 3.225, 3.11, 2.904)), row.names = c(NA, -92L), class = c("tbl_df", 
"tbl", "data.frame"))

试试这个:

p1 %>% ggplotly(tooltip = c("Date", "Rate")) %>% add_annotations(xref='paper',yref='paper',x=1,y=-0.07,text='*Source FRED',showarrow=F, font=list(size=10,color='black')) %>% add_annotations(xref='paper',yref='paper',x=1,y=1.035,text='*Core Inflation excludes food and energy items in consumer basket',showarrow=F, font=list(size=10,color='black'))

tooltip = c("Date", "Rate")位允许您指定要在图例中显示的变量

相关内容

  • 没有找到相关文章

最新更新