2

I have created a simple line chart with ggplot2 and would like to use geom_textpath to display the labelnames in the chart. The problem is that the labels currently overlap too much. How can I change this, either automatically or manually? Maybe there’s also a completely different and simple alternative?

library(ggplot2)
library(geomtextpath)

data <- structure(list(SCHULJAHR = c(2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2022, 
2022, 2022, 2022, 2022, 2022, 2022, 2023, 2023, 2023, 2023, 2023, 
2023, 2023, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2025, 2025, 
2025, 2025, 2025, 2025, 2025), Schulform = c("Berufskolleg", 
"Förderschule", "Gesamtschule", "Grundschule", "Gymnasium", 
"Hauptschule", "Realschule", "Berufskolleg", "Förderschule", 
"Gesamtschule", "Grundschule", "Gymnasium", "Hauptschule", "Realschule", 
"Berufskolleg", "Förderschule", "Gesamtschule", "Grundschule", 
"Gymnasium", "Hauptschule", "Realschule", "Berufskolleg", "Förderschule", 
"Gesamtschule", "Grundschule", "Gymnasium", "Hauptschule", "Realschule", 
"Berufskolleg", "Förderschule", "Gesamtschule", "Grundschule", 
"Gymnasium", "Hauptschule", "Realschule", "Berufskolleg", "Förderschule", 
"Gesamtschule", "Grundschule", "Gymnasium", "Hauptschule", "Realschule", 
"Berufskolleg", "Förderschule", "Gesamtschule", "Grundschule", 
"Gymnasium", "Hauptschule", "Realschule", "Berufskolleg", "Förderschule", 
"Gesamtschule", "Grundschule", "Gymnasium", "Hauptschule", "Realschule", 
"Berufskolleg", "Förderschule", "Gesamtschule", "Grundschule", 
"Gymnasium", "Hauptschule", "Realschule", "Berufskolleg", "Förderschule", 
"Gesamtschule", "Grundschule", "Gymnasium", "Hauptschule", "Realschule", 
"Berufskolleg", "Förderschule", "Gesamtschule", "Grundschule", 
"Gymnasium", "Hauptschule", "Realschule"), rel_change = c(0, 
0, 0, 0, 0, 0, 0, 2.01850697607518, -3.05949008498584, 2.11726384364821, 
3.94091136705058, -0.532820247169392, 0.143729787998563, 0.583279325988334, 
1.90823224816608, -3.28611898016997, 1.65038002171553, 5.13770655983976, 
-2.63450011100422, -0.395256916996047, -2.13869086195723, 1.60617538476291, 
1.58640226628895, 1.06406080347448, 5.45317976965448, -2.19788351957374, 
0.826446280991736, -1.50356448476993, 0.68082658100398, 6.85552407932011, 
1.12920738327904, 5.81872809213821, -2.21268408199512, 2.08408192597916, 
-1.98314970836034, -1.49590065685381, 8.95184135977337, 3.82193268186754, 
7.64146219328993, -2.27928661289129, -3.0901904419691, -2.099805573558, 
-1.24178932732416, 10.3116147308782, 11.4223669923996, 9.9649474211317, 
-2.10908014504551, -8.22853036291771, -3.05897602073882, -2.17193268447044, 
14.957507082153, 15.42888165038, 13.670505758638, 1.66506327240435, 
-7.22242184692778, -3.13674659753726, -0.455482571798437, 28.4985835694051, 
18.7296416938111, 18.9534301452178, 2.13128098867757, -7.36615163492634, 
-3.61633182112767, 0.469866231960493, 35.1841359773371, 19.3485342019544, 
20.9714571857787, 3.50033301265448, -4.4196909809558, -1.81464679196371, 
-1.99453420913842, 44.5892351274788, 22.5081433224756, NA, 5.38740472137941, 
-7.15055695292849, -0.0518470511989631)), row.names = c(NA, -77L
), class = c("tbl_df", "tbl", "data.frame"))


plot <- ggplot(data = data) +
  geom_line(aes(x = SCHULJAHR, y = rel_change, group = Schulform, colour = Schulform), size = 4.0, linetype ="solid") +
  geom_text(
    data = data %>%
      filter(SCHULJAHR %in% c(2025)),
    aes(
      x = SCHULJAHR, 
      y = rel_change, 
      label = sprintf("%.1f", rel_change),
      colour = Schulform   
    ),
    vjust = -0.5, hjust = 0.5, size = 5.0, fontface = "bold"
  ) +
 geom_textpath(
    aes(
      x = SCHULJAHR ,  
      y = rel_change,
      group = Schulform,
      colour = Schulform,
      label = Schulform
    ),
    size = 5.0,
    fontface = "bold",
    text_only = TRUE,
    hjust = 0.5,
    vjust = -0.2
  ) 
2
  • 4
    With that much overlapping lines it's hard to avoid any overlaps. Instead I would suggest to add the labels at the end of the lines using geom_text or ggrepel::geom_text_repel to avoid overlaps, e.g. something like i.sstatic.net/Jp7wKGl2.png Commented Nov 7 at 7:30
  • Hi Stefan, thank you for your feedback! Could you share the code you used for the geom_text (or geom_text_repel)? Commented Nov 7 at 10:01

1 Answer 1

5

You can try tweaking the values of the hjust argument (horizontal adjustment).

ggplot(data = data) + ...
geom_textpath(..., hjust = rep(c(.1, .9, .5, .9, .9, .8, .5), 11)) +
guides(colour=FALSE)

Giving:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.