I am facing problems on dodging a geom_text_repel
label on a graph. I looked other similar questions on the Forum but I was not able to understand exactly how to force the dodging on these labels.
Nuge_x
and Nudge_y
are not compatible with position_dodge(0.3)
, but also position_dodge(0.3)
on the labels is not working as well.
Is there a method to force it? I tried force =20
unsuccesfully.
Below you can see the code with the test I made and the graph showund the problem.
# Dataframe creation
Dati2 <- data.frame(
Definition = c("1 min, 40°C", "5 min, 40°C", "1 min, 40°C", "5 min, 40°C", "1 min, 40°C", "5 min, 40°C"),
Grade = c("A", "A", "B", "B", "C", "C"),
Run = c(35, 35, 46, 46, 46, 46),
Note = c(NA, NA, "Cade dopo 10''", "Cade dopo 10''", "Cade dopo 8''", "Cade dopo 8''")
)
# Print of Dataframe
print(Dati2)
library(scales)
library(ggrepel)
library(readxl)
library(dplyr)
library(ggplot2)
library(ggpubr)
theme_set(theme_pubclean())
# Graph coding
Graph2 <- ggplot(Dati2, aes(Definition, Run)) +
geom_linerange(
aes(x = Definition, ymin = 0, ymax = Run, group = Grade),
color = "lightgray", size = 1.5,
position = position_dodge(0.3)
)+
geom_point(
aes(color = Grade),
position = position_dodge(0.3), size = 3
)+
scale_color_manual(values = c("#0073C2FF", "#EFC000FF", "#4ade40"))+
coord_flip() +
theme(
axis.text.y = element_text(angle = 45, hjust = 1), # Inclination of text y
axis.title.y = element_blank(), # Remoxe axis title
axis.title.x = element_text(size = 12), # modify axis title
legend.title = element_blank(), # Remove legend titlelegenda
) +
geom_text_repel(
aes(label = Note),
size = 3,
# nudge_x = 0.5,
# nudge_y = 0.5,
box.padding = 1,
point.padding = 0.2,
max.overlaps = 10,
na.rm = TRUE,
min.segment.length = 0,
position = position_dodge(0.3),
force =20
)
Graph2
You need to add group = Grade
to the mapping for geom_text_repel
, and the labels should be dodged properly.
...
geom_text_repel(
aes(label = Note, group = Grade),
...
)
You can also use col
aesthetic instead of group
for the labels to have the same color as the points.