Overlapping labels with geom_dl()
Asked Answered
K

2

5

My line plot is suitable except my labels overlap too much. I realize their respective datapoints are close, but is there a way to pretty up the label spacing?

My data and code:

library(ggplot2)
library(directlabels) #geom_dl

#Original df
df <- data.frame(names=c('AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE'),SP22=c(57, 30, 27, 35, 34),FA22=c(52, 38, 31, 34, 31),SP23=c(49, 32, 30, 29, 31))

#df to long format
longdf <- melt(df, id='names')

last.bumpup <- list("last.points","bumpup")

#lineplot
longdf %>%
ggplot() + 
  geom_line(aes(x=variable, y=value, group=names, color=names)) +
  scale_y_continuous(n.breaks=6, limits=c(20, 60)) +
  scale_color_manual(values=c("darkred", "steelblue","black", "coral1", 
                              "darkorchid2")) +
  geom_dl(aes(x=variable, y=value,label=names, color= names),
          method="last.bumpup", cex=0.8) +
  labs(title = "Comparison of...") + 
  xlab(label = "Terms") +
  ylab(label = "Numbers") +
  #scale_colour_discrete(guide="none") +
  theme_minimal() +
  theme(legend.position = "none")

What do I need to add to/change in my code to achieve prettier label positioning? My sincere thanks in advance.

Kaila answered 25/5, 2023 at 19:29 Comment(0)
T
5

Another option is direct labelling using geom_textline from the geomtextpath package:

library(geomtextpath)

longdf %>%
  ggplot(aes(variable, value, group = names, colour = names)) + 
  geom_textline(aes(label = names, hjust = names), spacing = 200) +
  scale_hjust_manual(values = c(0.1, 0.4, 0.1, 0.6, 0.3)) +
  scale_y_continuous(n.breaks = 6, limits = c(20, 60)) +
  scale_color_manual(values = c("darkred", "steelblue","black", "coral1", 
                                "darkorchid2"), guide = "none") +
  labs(title = "Comparison of...", x = "Terms", y = "Numbers") + 
  theme_minimal() 

enter image description here

Tobey answered 25/5, 2023 at 20:11 Comment(2)
Ah, that is a nice solution, @Allan! Is it possible to space the label space (e.g., AAAA becomes A A A A)?Kaila
@Kaila sure, just add spacing = 200 (or whatever number you like) - see my updateTobey
K
5

One option is to use geom_label_repel from ggrepel. Doing this is easier if you subset the points that you want to label beforehand.

library(ggrepel)

label.sub <- longdf[longdf$variable=="SP23",]

#lineplot
longdf %>%
  ggplot() + 
  geom_line(aes(x=variable, y=value, group=names, color=names)) +
  scale_y_continuous(n.breaks=6, limits=c(20, 60)) +
  scale_color_manual(values=c("darkred", "steelblue","black", "coral1", 
                              "darkorchid2")) +
  geom_label_repel(data=label.sub, aes(x=variable, y=value, label=names, colour=names), 
                   hjust=0, direction="y", segment.colour = "lightgrey", nudge_x = 0.1) +
  labs(title = "Comparison of...") + 
  xlab(label = "Terms") +
  ylab(label = "Numbers") +
  #scale_colour_discrete(guide="none") +
  theme_minimal() +
  theme(legend.position = "none")

enter image description here

You can also play with various arguments to get the spacing you want - see the documentation for details. Briefly, nudge_x for left/right spacing from the line ends, and box.padding for space around each label.

Karlenekarlens answered 25/5, 2023 at 19:54 Comment(0)
T
5

Another option is direct labelling using geom_textline from the geomtextpath package:

library(geomtextpath)

longdf %>%
  ggplot(aes(variable, value, group = names, colour = names)) + 
  geom_textline(aes(label = names, hjust = names), spacing = 200) +
  scale_hjust_manual(values = c(0.1, 0.4, 0.1, 0.6, 0.3)) +
  scale_y_continuous(n.breaks = 6, limits = c(20, 60)) +
  scale_color_manual(values = c("darkred", "steelblue","black", "coral1", 
                                "darkorchid2"), guide = "none") +
  labs(title = "Comparison of...", x = "Terms", y = "Numbers") + 
  theme_minimal() 

enter image description here

Tobey answered 25/5, 2023 at 20:11 Comment(2)
Ah, that is a nice solution, @Allan! Is it possible to space the label space (e.g., AAAA becomes A A A A)?Kaila
@Kaila sure, just add spacing = 200 (or whatever number you like) - see my updateTobey

© 2022 - 2024 — McMap. All rights reserved.