简体   繁体   中英

How to remove legend entry in hovering display when using ggplotly

I have created the following graph using ggplotly . I am looking for a solution whereby I can remove the legend entry in the hovering display. I would only like the hovering display to show the date (x axis) and value (y axis).

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

Here is a reproductible of my dataset

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

Try this:

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

The tooltip = c("Date", "Rate") bit allows you to specify which variables you'd like to show up in the legend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM