标签geom_step系列(组),但不是所有点

时间:2018-11-03 03:24:25

标签: r ggplot2 ggrepel

如何在不标记每个点的情况下标记每个geom_step系列(group)?这是要标记的图:

my code

ggrepel是我的收获:

enter image description here

ex <- structure(list(date = structure(c(17643, 17650, 17657, 17664, 
                                        17671, 17678, 17685, 17692, 
                                        17699, 17706, 17713, 17720, 17727, 
                                        17734, 17741, 17748, 17755, 17762, 
                                        17769, 17776, 17783, 17790, 
                                        17797, 17804, 17811, 17818, 17825, 
                                        17832, 17643, 17650, 17657, 
                                        17664, 17671, 17678, 17685, 17692, 
                                        17699, 17706, 17713, 17720, 
                                        17727, 17734, 17741, 17748, 17755, 
                                        17762, 17769, 17776, 17783, 
                                        17790, 17797, 17804, 17811, 17818, 
                                        17825, 17832, 17643, 17650, 
                                        17657, 17664, 17671, 17678, 17685, 
                                        17692, 17699, 17706, 17713, 
                                        17720, 17727, 17734, 17741, 17748, 
                                        17755, 17762, 17769, 17776, 
                                        17783, 17790, 17797, 17804, 17811, 
                                        17818, 17825, 17832), class = "Date"), 
                     value = c(13, 23, 18, 18, 19, 16, 20, 17, 13, 12, 10, 7, 
                               17, 15, 40, 28, 38, 28, 48, 33, 24, 28, 43, 45, 
                               58, 47, 57, 21, 1, 1, 0, 2, 0, 0, 3, 1, 2, 2, 0, 
                               0, 5, 6, 10, 7, 7, 6, 8, 3, 12, 8, 5, 11, 10, 7, 
                               22, 10, 26, 26, 19, 15, 9, 6, 1, 4, 146, 230, 
                               105, 151, 77, 322, 170, 148, 236, 242, 242, 
                               223, 171, 247, 301, 491, 479, 560, 402, 51), 
                     label = c(rep("A",28), rep("B", 28), rep("C", 28))), 
                row.names = c(NA, -84L), 
                class = c("tbl_df", "tbl", "data.frame"))

library(tidyverse)
library(ggrepel)
ggplot(ex, aes(x = date, y = value, group=label)) +
  geom_step(aes(color=label)) +
  theme_bw() +
  theme(legend.position = "none") +
  scale_x_date(breaks = "1 month", date_labels="%m-%y") +
  geom_label_repel(aes(label = label),
                   nudge_x = 1,
                   na.rm = TRUE)

1 个答案:

答案 0 :(得分:3)

随机选择三个日期怎么样?

set.seed(1)
n <- length(unique(ex$label))
idx <- sample(1:(nrow(ex) / n), n) + 0:(n - 1) * (nrow(ex) / n)
ggplot(ex, aes(x = date, y = value, group=label)) +
  geom_step(aes(color=label)) +
  theme_bw() +
  theme(legend.position = "none") +
  scale_x_date(breaks = "1 month", date_labels="%m-%y") +
  geom_label(data = ex[idx, ], aes(label = label))

enter image description here