简体   繁体   中英

How to hide information on graph from the Legend in ggplotly?

I have horizontal dots plot replotted via ggplotly

df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
              Percentage = c(23,16,32,71,3,60,15,21,44,60),
              rate = c(10,12,20,200,-25,12,13,90,-105,23),
              change = c(10,12,-5,12,6,8,0.5,-2,5,-2)

library(ggplot2)

plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
  geom_hline(aes(yintercept = origin), color = 'gray') +
  geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
  geom_point(aes(color = 'Rate'), size = 10) +
  geom_text(aes(label = rate), color = 'white') +
  geom_point(aes(x = change, color = 'Change'), size = 10) +
  geom_text(aes(label = change, x = change)) +
  theme_minimal(base_size = 16) +
  scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
  scale_color_manual(values = c('#aac7c4', '#5f9299')) +
  theme(panel.grid = element_blank(),
        axis.text.y = element_text(color = 'gray50')) +
  labs(color = NULL, y = NULL)

ggplotly(plt)

在此处输入图像描述

The only issue is that when I hide one of the dot from the figure, texts are still appeared (see below), so is there way to tackle this issue and hide text with circle by clicking on legend?

PS (setting text colour in white color = 'white' is not option for me)

在此处输入图像描述

You can use the fill aesthetic for the points (as long as they are shape = 21) and use the color aesthetic for the text. As long as these have the same labels for aesthetic mapping, the interactivity for both points and text will be linked.

One minor annoyance is that this changes the plotly legend labels, even though they are correct in the ggplot version. This requires a little direct manipulation of the plotly object itself:

plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
  geom_segment(aes(x = -100, xend = 200,
                   y = origin, yend = origin), color = 'gray') +
  geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
  geom_point(aes(fill = 'Rate'), shape = 21, size = 10, color = NA) +
  geom_text(aes(label = rate, color = 'Rate')) +
  geom_point(aes(x = change, fill = 'Change'), 
             color = NA, shape = 21, size = 10) +
  geom_text(aes(label = change, x = change, color = "Change")) +
  theme_minimal(base_size = 16) +
  scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
  scale_fill_manual(values = c('#aac7c4', '#5f9299')) +
  scale_color_manual(values = c("black", "white")) +
  theme(panel.grid = element_blank(),
        axis.text.y = element_text(color = 'gray50')) +
  labs(color = NULL, y = NULL, fill = NULL)

p <- ggplotly(plt)

p$x$data[[3]]$name <- p$x$data[[3]]$legendgroup <-
  p$x$data[[4]]$name <- p$x$data[[4]]$legendgroup <- "Rate"
p$x$data[[5]]$name <- p$x$data[[5]]$legendgroup <-
  p$x$data[[6]]$name <- p$x$data[[6]]$legendgroup <- "Change"

p

This gives us the following plot:

在此处输入图像描述

Now clicking on Rate we get:

在此处输入图像描述

And clicking on Change we get

在此处输入图像描述

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