How to label lines obtained using split with ggrepel

I am trying to label 4 lines grouped by the value of variable cc . To label the lines I use ggrepel but I get all the 4 labels instead of 2 for each graph. How to correct this error?

The location of the labels is in this example at the last date but I want something more flexible: I want to locate each of the 4 labels in specific points that I chose (e.g. b at date 1, a at date 2, etc.). How to do that?

library(tidyverse)
library(ggrepel)
library(cowplot)

set.seed(1234)
df <- tibble(date = c(rep(1,4), rep(2,4), rep(3,4), rep(4,4)),
             country = rep(c('a','b','c','d'),4),
             value = runif(16),
             cc = rep(c(1,1,2,2),4))

df$cc <- as.factor(df$cc)

# make list of plots
ggList <- lapply(split(df, df$cc), function(i) {
  ggplot(i, aes(x = date, y = value, color = country)) +
    geom_line(lwd = 1.1) +
    geom_text_repel(data = subset(df, date == 4),
                    aes(label = country)) +
    theme(legend.position = "none")
})

# plot as grid in 1 columns
cowplot::plot_grid(plotlist = ggList, ncol = 1,
                   align = 'v', labels = levels(df$cc))


Created on 2021-08-18 by the reprex package (v2.0.0)

An answer to my question is on r - How to label lines obtained using split with ggrepel - Stack Overflow

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.