R ggplot: geom_text gets positioned in a single line and does not follow data points

Viewed 36

I am trying to plot the odds ratios in a forest plot fashion - this is a kind of data representation that consensually appears the most feasible in my case. However, I bump into the problem of geom_text aligning into one line and not following the data points. Are there any suggestions on what I could do with this?

Here's the code to reproduce what I'm trying to do:

label <- c("C-terminal", "Inactivation gate", "N-terminus", "S1-S3", "S3-S4 linker", "S4", "S4-S5 linker", "S5",
           "S5-S6 linker", "S6", "Unconserved linker",
           "C-terminal", "Inactivation gate", "N-terminus", "S1-S3", "S3-S4 linker", "S4", "S4-S5 linker", "S5",
           "S5-S6 linker", "S6", "Unconserved linker",
           "C-terminal", "Inactivation gate", "N-terminus", "S1-S3", "S3-S4 linker", "S4", "S4-S5 linker", "S5",
           "S5-S6 linker", "S6", "Unconserved linker",
           "C-terminal", "Inactivation gate", "N-terminus", "S1-S3", "S3-S4 linker", "S4", "S4-S5 linker", "S5",
           "S5-S6 linker", "S6", "Unconserved linker"
)
phenotype <- c("DS", "DS", "DS", "DS", "DS", "DS", "DS", "DS",
               "DS", "DS", "DS",
               "eDEE", "eDEE", "eDEE", "eDEE", "eDEE", "eDEE", "eDEE", "eDEE",
               "eDEE", "eDEE", "eDEE",
               "GEFSp", "GEFSp", "GEFSp", "GEFSp", "GEFSp", "GEFSp", "GEFSp", "GEFSp",
               "GEFSp", "GEFSp", "GEFSp",
               "HM", "HM", "HM", "HM", "HM", "HM", "HM", "HM",
               "HM", "HM", "HM")
odds <- as.numeric(c("0.3847331",  "1.1218567",  "0.7011586",  "1.2048013",  "0.4799130",  "2.1794015",  "1.6336479",  "1.6019002",  
                     "3.1062446",  "4.8326506",  "0.2029079",  "0.0000000",
                     "2.3899286",  "0.0000000",  "0.3641355",  "0.0000000",  "3.9357402",  "0.0000000",  "5.1269997",  
                     "2.4349689",  "0.0000000",  "0.9392773",  "0.6059535",  "1.3119187",
                     "0.9347497",  "1.1454902",  "0.5393331",  "1.8247280",  "2.1722995",  "2.1775909",  "2.3831240",  
                     "5.0178285",  "0.1593788",  "0.0000000", "14.6582596",  "0.0000000",
                     "0.4421253",  "0.0000000",  "4.8406276",  "8.1111606",  "3.9470930",  "0.0000000",  "7.4429864",  "0.1343254"))
lCI <- as.numeric(c("0.277568005", "0.621926336", "0.480384784", "0.938653693", "0.164801310", "1.391938191", "1.009118187",
                    "1.040073330", "2.480388200", "2.951950890", "0.157151171",
                    "0.000000000", "0.055082172", "0.000000000", "0.008651769", "0.000000000", "0.421730054", "0.000000000",
                    "0.914263782", "0.669334924", "0.000000000", "0.286774804",
                    "0.296884611", "0.326472332", "0.401513840", "0.651927445", "0.012710448", "0.665154352", "0.842968797",
                    "0.944153869", "1.506389805", "2.190553413", "0.073570664",
                    "0.000000000", "3.186118715", "0.000000000", "0.010382868", "0.000000000", "0.509813871", "1.399372224",
                    "0.418500986", "0.000000000", "0.768730012", "0.003168735"))

uCI <- as.numeric(c("0.5282010",  "2.0308771",  "1.0172689",  "1.5474852",  "1.2568157",  "3.4731289",  "2.6793281",  "2.4911898",
                    "3.9037593",  "8.2489699",  "0.2601633",  "1.3495566",
                    "16.4933450",  "2.8762348",  "2.3591293", "17.4014715", "17.9075121",  "7.8283892", "19.2225415",  "7.4175071",
                    "11.9679019",  "2.7311742",  "1.1344294",  "3.8882872",
                    "1.9320901",  "1.9289863",  "3.5630593",  "4.3289498",  "4.9732189",  "4.5882830",  "3.7098054", "11.0053383",
                    "0.3085142",  "1.6536415", "54.0260666",  "3.5234297",
                    "2.9482601", "21.2689112", "22.8034852", "32.1967578", "18.3833219",  "1.7913029", "36.2843119",  "0.8897025"))


df <- data.frame(label,phenotype,odds)

df %>% mutate(label = factor(label, levels = unique(label))) %>% 
  ggplot(aes(x = odds, y = label))+
  geom_vline(xintercept = 1, linetype = "dotted", color = "grey", size = 1.4)+
  geom_point(aes(color = phenotype), size = 3, position = position_dodge(width = 0.9))+
  geom_text(aes(label=formatC(odds,digits=3)), size=3, position = position_dodge(0.9)) +
  theme_bw(base_size = 20)+
  scale_color_manual(values = c("#AAE9E5","#87C7F1","#FEB7D3","#FFEDA9","#EACFFF","#A4DEAD"), name = "Phenotypes")+
  scale_x_continuous(trans = "log10", breaks = c(0.01,0.1,1,10,100))+
  geom_errorbar(aes(xmin = lCI, xmax = uCI), position = position_dodge2(0.7), width = 0.9)+
  theme(legend.position = "bottom",
        legend.text=element_text(size=8),
        legend.title=element_text(size=8),
        panel.border = element_blank(),
        axis.text = element_text(color = "#303030", size = 8),
        axis.ticks.x = element_line(color = "#303030", size = 1),
        axis.ticks.y = element_blank(),
        axis.title = element_text(color = "#303030", size = 10),
        plot.title = element_text(color = "#303030", hjust = 0.5,size=14, face="bold"),
        plot.subtitle = element_text(color="#2b5859",hjust = 0.5, size=10))+
  labs(x = expression("Odds ratio (log"[10]*")"),
       y = "Protein domain",
       title = "Odds ratios",
       colour="Legend"
  )+
  coord_cartesian(xlim = c(0.008,150))

And here's the result. I would like the data point text to be positioned exactly over the data points, but now they are positioned in a single line.

Thank you in advance for your help!

enter image description here

0 Answers
Related