Home > Software engineering >  How to insert specific IDs in the scatterplot in R
How to insert specific IDs in the scatterplot in R

Time:03-26

Here an example of my data:

mydf <- tibble(
  ID = c(1, 2, 9,4,5,15),
  M1 = c(60,50,40,20,30,45),
  M2 = c(90,80, 30, 33,70,40)
)

I want to insert only IDs in the plot in which M1<M2.

Here are the codes that I have used:

 library(ggrepel)
    res1<- mydf %>% 
      filter(M1< M2)
    p <- ggplot(data = mydf, aes(x =  M1, y =    M2,label = ID)) 
      geom_text_repel()  
      geom_point(data=res1, 
                 aes(x=M1,y= M2),                       
                 color='red',
                 size=3)

I want to remove IDs 9,15 as M1>M2.

CodePudding user response:

Does this give you the desired plot?

mydf <- mydf %>% 
  mutate(M1lower = M1 < M2)

res1<- mydf %>% 
  filter(M1lower == T)

ggplot(data = mydf, aes(x =  M1, y =M2,label = ID, color = M1lower)) 
  geom_point(size=3)  
  geom_text_repel(data=res1, 
                  aes(x=M1,y= M2))

enter image description here

  •  Tags:  
  • r
  • Related