0

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

Orla_wang
  • 1
  • 2
  • Could you please share some reproducible data using `dput`? – Quinten Oct 01 '22 at 16:45
  • Hi, I just upload my data and the whole code,see the hyperlinks at the end of the article,thank you ! – Orla_wang Oct 02 '22 at 08:04
  • My understanding of ggrepel is that it is specifically designed *not* to put the text in the coordinates you give, but to *repel* text "away from each other and away from the data points". geom_text would put text exactly where you want. Having text exactly at the position _pointed to_ by the arrow needs a bit more computing. vegan:::ordiArrowTextXY shows how to do this in conventional R graphics. Trickier in ggplot2, and ggvegan2::geom_ordiarrow (in github) just defaults to geom_text with vjust="outwards", hjust="outwards". – Jari Oksanen Oct 03 '22 at 06:54
  • Hi,you just inspired me. geom_text_repel(aes( ) ) can adjustify text position .what i need is a little bit calculation which i just figure out. And i change geom_text_ggrepel() back to geom_text(), thank you for helping me. – Orla_wang Oct 03 '22 at 09:36

1 Answers1

0

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

Orla_wang
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 06 '22 at 09:43