r rda vegan ggplot geom_text. I wanna specify the label position to put it exactly where the arrows point

403 Views Asked by At

This is what I want to put the text label to the position where the arrows point:

This is what I want to put the text label to the position where the arrows point

These are my results:

results

I want to put the text label to the position where the arrows point, I used library(ggrepel).

My code is

  p <- ggplot(rda_tb_fs.site, aes(RDA1, RDA2)) +
       geom_vline(xintercept = 0, color = 'gray', size = 0.5) + 
       geom_hline(yintercept = 0, color = 'gray', size = 0.5) +
       geom_segment(data = rda_tb_fs.env,
           aes(x = 0,y = 0, xend = RDA1,yend = RDA2), 
           arrow = arrow(length = unit(0.4, 'cm'),
                         angle=20,
                         type = "open"), 
           size = 1,
           lineend="round",
           linejoin="mitre",
           color='blue'
             ) +
       geom_text_repel(data = rda_tb_fs.env, 
                      aes(RDA1*1.06, RDA2 *1.05,
                          label=c("K","pH","ORP",
                                 "HCO3","WTO")
                        ))

I understand when RDA1 is positive then what I need is RDA1+0.1,but when RDA1 is negtive what i need is RDA1-0.1,i just don't known how to put this conditional statements into use.

this is data 1 which i made some reforms under the principle of confidentiality

data 2

this is the original R code i wrote

1

There are 1 best solutions below

1
Orla_wang On

I changed change geom_text_ggrepel() back to geom_text(),and made some modification in geom_text(aes()) and it works:

  geom_text(data = rda_tb_fs.sp, 
        aes(
        ((RDA1^2+RDA2^2)^0.5+0.15)/(RDA1^2+RDA2^2)^0.5*RDA1, 
        ((RDA1^2+RDA2^2)^0.5+0.15)/(RDA1^2+RDA2^2)^0.5*RDA2,
            label=c("Cd","Cr","Zn","Ni",
                    "Cu","Pb","Co","Fe",
                    "Mn","As","Ti"
                    )
        ),
        size =5,
        fontface = "bold",
        family="Times New Roman",
        color='red',
        parse = T,
        )

this is my result