Hi!
I am trying to make a scatterplot with ggplot
. I have subset my point into 4 groups based on p.value and range of log2fc. I used the geom_text_repel
to avoid overlapping text and it works within the same "group" but among different groups some text overlaps.
here my code (apologies if your eyes are bleeding):
#scatterplot
library(ggplot2)
library(ggrepel)
#with pvalue vbut less logf2fc highlighted
dfup <- text %>% filter(log2FC >= 1.5 & p_value <= 0.05) #ok
pvalup<- text %>% filter(p_value <= 0.05 & log2FC > 0 & log2FC <= 1.4)
pvaldown<- text %>% filter(p_value <= 0.05 & log2FC >= -1.4 & log2FC < 0)
dfdown <- text %>% filter(log2FC <= -1.5 & p_value <= 0.05) #ok
ggplot(data = text, aes(x= log2FC, y= -1*log10(p_value))) +
geom_point() +
geom_point(data= pvaldown, aes(x= log2FC, y= -1*log10(p_value)), color = "dodger blue")+
geom_point(data= pvalup, aes(x= log2FC, y= -1*log10(p_value)), color = "magenta")+
geom_point(data= dfup, aes(x= log2FC, y= -1*log10(p_value)), color = "red")+
geom_point(data= dfdown, aes(x= log2FC, y= -1*log10(p_value)), color = "blue")+
geom_vline(xintercept = 1.5,linetype = "dashed", color = "dim gray")+
geom_vline(xintercept = -1.5,linetype = "dashed", color = "dim gray")+
geom_hline(yintercept = -1*log10(0.05),linetype = "dashed", color = "dim gray") +
geom_hline(yintercept = -1*log10(0.01),linetype = "dotted", color = "gray")+
geom_text(mapping=aes(x=-10,y=2.0),label=paste("0.01"), color = "gray", size = 2.5, vjust=-0.5)+
geom_hline(yintercept = -1*log10(0.001),linetype = "dotted", color = "gray") +
geom_text(mapping=aes(x=-10,y=3.0),label=paste("0.001"), color = "gray", size = 2.5, vjust=-0.5)+
geom_text_repel(data=dfdown, aes(label=`Associated.Gene.Name`), color = "blue")+
geom_text_repel(data=pvalup, aes(label=`Associated.Gene.Name`), color = "magenta")+
geom_text_repel(data=pvaldown, aes(label=`Associated.Gene.Name`), color = "dodger blue")+
geom_text_repel(data=dfup, aes(label=`Associated.Gene.Name`), color = "red")+
theme_bw(base_size = 12)+ #size of the labelling +
theme( title = element_text(hjust = 0.5), axis.title = element_text(color = "black"),panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
what I can do to avoid the overlapping of the plots?
Thank you!
Camilla
If you are using multiple
geom_text_repel
in one plot you can try to set a specific direction for each set so they don’t collide. Check this Stackoverflow post to see if it answers your question.Could you share a small example of your dataset ?
dput(head(your_df))
would be great to reproduce your issuesure. row are the genes and the columns the samples.