rggplot2geom

Labels in ggplot2 Are Misplacing Dots When Adding Text Labels


I am trying to add text labels to points in a scatter plot using ggplot2 in R. However, after adding the text labels, the dots associated with those labels are getting misplaced. Here's a simplified version of my code:

library(ggplot2)
library(ggrepel)
library(dplyr)

df = data.frame(structure(list(FC = c(4.712, 4.8264, 0.078026, 0.098754, 11.612, 
0.21181, 6.1715, 0.15113, 0.19936, 0.23572, 0.23564, 6.9851, 
5.2465, 12.641, 4.4164, 0.24834, 7.4598, 5.1384, 0.18861, 0.24986
), log2FC = c(2.2363, 2.271, -3.6799, -3.34, 3.5375, -2.2391, 
2.6256, -2.7261, -2.3266, -2.0849, -2.0853, 2.8043, 2.3914, 3.6601, 
2.1429, -2.0096, 2.8991, 2.3613, -2.4065, -2.0008), Identifier = 1:20), row.names = c("3-Indoleacetic acid", 
"4-Hydroxyphenylpyruvic acid", "Adenine", "Adenosine", "alpha-Ketoglutaric acid", 
"C10", "C18:1OH", "Deoxyadenosine", "Deoxycytidine", "Deoxyguanosine", 
"Histamine", "Homocitrulline", "Indolelactic acid", "Kynurenine", 
"Leucine", "LysoPC a C20:4", "N-Acetylputrescine", "Phenylalanine", 
"Phenylethylamine", "Spermidine"), class = "data.frame"))


show.names = c("Kynurenine", "alpha-Ketoglutaric acid", "N-Acetylputrescine", "Homocitrulline", "C18:1OH", "Deoxycytidine", "Phenylethylamine", "Deoxyadenosine", "Adenosine", "Adenine")

p1 = ggplot(df, aes(x = Identifier, y = log2FC)) +
     geom_point(aes(color = log2FC >= 0)) +
     scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("Down", "Up")) +
     geom_text_repel(data = df[rownames(df) %in% show.names,], aes(label = show.names)) +
     geom_hline(yintercept = 0, linetype = "solid") +
     labs(x = "Compounds", y = "Log2 Fold Change") + theme_minimal()
p1

output

In the resulting plot, the points are not aligned correctly with their labels. For example, "Kynurenine" has positive values (3.6601), but they are displayed in negative value (near -4) area of the plot

Could anyone help me understand why the dots are getting misplaced when I add text labels, and how I can fix this issue?


Solution

  • The issue with misaligned points and text labels in your ggplot2 plot seems to be due to the way the labels are being assigned in the geom_text_repel function. Specifically, the show.names vector is not directly aligned with the data frame rows, causing the text labels to be misplaced.

    To fix this, you should use the label aesthetic within geom_text_repel and ensure it correctly matches the rows based on their names.

    ggplot(df, aes(x = Identifier, y = log2FC)) +
         geom_point(aes(color = log2FC >= 0)) +
         scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("Down", "Up")) +
         geom_text_repel(data = subset(df, rownames(df) %in% show.names), aes(label = rownames(subset(df, rownames(df) %in% show.names)))) +
         geom_hline(yintercept = 0, linetype = "solid") +
         labs(x = "Compounds", y = "Log2 Fold Change") + 
         theme_minimal()
    

    output