在绘图中更改geom_text标签角度

时间:2018-09-30 13:42:02

标签: r ggplot2 plotly

我有使用ggplot + ggplotly构建的以下图表。

我尝试在红色(中位数)和蓝色(百分之90%)的垂直线上添加标签,而没有运气。 请告知我该如何解决。

我使用的代码:

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)

我希望文本是垂直的,但是使用How to add legend for vertical lines in ggplot?的答案对我没有帮助。

enter image description here

0 个答案:

没有答案