Hello
I have been using ggplot2 to plot two sets of values from a data table called "TableFCvsFC". The two parameters that I am drawing are values from the column "SM" vs values from the column "SPL", which correspond to the same gene expression evaluated in different conditions. As I don't know how to import the data table to this post, I paste an image of a fragment of the table as an example:
So far I have used the next code to draw the following graph:
library(ggplot2)
Change <- openxlsx::read.xlsx("/Users/Desktop/TableFCvsFC.xlsx")
LC <- ggplot(Change, aes(x=SM, y=SPL, family = "sans"))
LC + geom_vline(xintercept = 1e+00) +
geom_hline(yintercept = 1e+00) +
geom_abline(intercept = 0, slope = 1) +
geom_point(size=2, shape=16, alpha = 1, color = "black") +
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10")
What I need now is to highlight the dots located inside the orange rectangle by coloring them in red, while the rest of the dots should remain black. The particular characteristic of the dots to be red is that the value in "SM" column should be higher than 2, while the value in "SPL" column should be lower than 1. I suppose that one thing that I can do is to create a new vector with the rows that meet those requirements and then customize the colour according to that variable, but I don't know how write that part of the code.
What I also would like to do is to add a label to the 10 red dots with the highest values in "SM". Those labels should contain the corresponding "GeneName" for each of those dots.
Can anybody help me? As I do not provide the complete data table (sorry for that), you can create a similar data table with random values to evaluate the code. I can then adapt it to my own data.
Your
geom_point
line should be the following to color the values where SM > 2 and SPL < 1.If you want the dots that satisfy the above condition to be red and the rest black you can modify the color palette manually.
To label the top SM values where SM > 2 and SPL < 1, first find the genes where this is true, and then add a text layer using
ggrepel
.Your final code should look something like this.
Thank you so much for your help rpolicastro ! I will try that code! :D
One more question if you have time to answer: in the case I would like to color dots "SPL >2 & SM < 1" in blue at the same time the other dots "SM > 2 & SPL < 1" are red and the rest of the dots are black, how should I proceed? I would also like to label top 10 blue dots in that case.