3

I have the following chart I have built using ggplot + ggplotly.

I am trying to add labels to the red (median) and blue (percentile 90%) vertical lines without luck. Please advise how should I fix it.

The code I have used:

p1 <- ggplot(users_d_total %>% filter(isSame, D_rank == 2), aes(x = D, fill = as.factor(train_user_id))) +
  geom_density(alpha = .3) +
  labs(title = paste0("Without Normalization Analysis [K = 2]")) + 
  scale_fill_discrete(name = "Users") +
  scale_x_continuous(breaks = by_two) +
  geom_vline(aes(xintercept = median(D)), col = 'red', linetype = 1, size = 1) + 
  geom_text(aes(x = median(D), y = 1, label = "Median"), hjust = 1, angle = 90, colour= "red") + 
  geom_vline(aes(xintercept = quantile(D, probs = .9)), col = 'blue', linetype = 1, size = 1) +
  geom_text(aes(x = quantile(D, probs = .9), y = 1, label = "90th Percentile"), hjust = 1, angle = 90, colour = "blue") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(p1)

I want the text to be vertical but using the answer from How to add legend for vertical lines in ggplot? didn't help me.

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
SteveS
  • 3,789
  • 5
  • 30
  • 64
  • 1
    Does it work without plotly? – pogibas Sep 30 '18 at 13:46
  • @PoGibas sure it does, I need these labels + plotly functionality. – SteveS Sep 30 '18 at 13:48
  • 1
    This is not possible until [this gets fixed](https://github.com/ropensci/plotly/issues/709). (MWE: `plotly::ggplotly(ggplot() + geom_text(aes(1, 1, label = 1), angle = 90))`) – pogibas Sep 30 '18 at 13:52
  • Thanks @PoGibas it's more than a year after this bug have been reported and no one fixed it.... Any suggestions how to solve it? Maybe something creative you can teach me to bypass? – SteveS Sep 30 '18 at 13:55
  • 1
    `+ coord_flip()` :-) And first of all, I would use boxplot/violinplot. Or add annotation of `geom_vline` to legend – pogibas Sep 30 '18 at 13:57
  • 1
    Also, just a general tip, but you might try to `log2` x-axis for visualization purpose. – pogibas Sep 30 '18 at 14:03
  • 1
    Yes I am aware :) hope one day they will fix it. – SteveS Sep 30 '18 at 14:05

0 Answers0