火山图中的颜色调节基因 - ggplot2

时间:2022-01-26 19:37:18

Hi I'm very new in R and I'm struggling trying to modify an R code that I found on internet when learning how to make a volcano plot.

嗨,我是R的新手,我正在努力修改我在互联网上找到的R代码,学习如何制作火山情节。

This code is to make volcano plots using ggplot2 and the problem I have is that I want to colour the up- and down-regulated proteins instead of colouring the proteins above the specified threshold. The code I'm using is the following:

这个代码是使用ggplot2制作火山图,我遇到的问题是我想为上调和下调蛋白着色,而不是将蛋白着色到指定阈值以上。我正在使用的代码如下:

install.packages("ggplot2")

gene_list <- read.table("/Users/Javi/Desktop/gene_list.csv", header=T, sep=",")

require(ggplot2)
##Highlight genes that have an absolute fold change > 2 and a p-value < 0.05
gene_list$threshold = as.factor(abs(gene_list$logFC) > 2 & gene_list$P.Value < 0.05)

##Construct the plot object
g = ggplot(data=gene_list, aes(x=logFC, y=-log10(P.Value), colour=my_palette)) +
    geom_point(alpha=0.4, size=5) +
    theme(legend.position = "none") +
    xlim(c(-10, 10)) + ylim(c(0, 15)) +
    xlab("log2 fold change") + ylab("-log10 p-value")
g

What I would like to do is to colour in red (for example) the logFC values > 1.3 and in blue the logFC values < -1.3

我想要做的是以红色(例如)logFC值> 1.3着色,以及蓝色logFC值<-1.3

The csv file I'm using is just an example and would be something like this:

我正在使用的csv文件只是一个例子,它将是这样的:

    logFC   P.Value
a   2       0.04
b   5       0.04
c   8       0.04
d   4       0.000005
e   7       0.01
f   1       0.04
g   -6      0.0001
h   -8      0.04

Thanks very much for your help in advance. Cheers Javi

非常感谢您的帮助。干杯哈维

1 个解决方案

#1


1  

Create a new color flag on your dataframe:

在数据框上创建一个新的颜色标记:

gene_list$color_flag <- ifelse(gene_list$logFC > 1.3, 1, ifelse(gene_list$logFC < -1.3, -1, 0))

Then add fill = color_flag to your aes.

然后将fill = color_flag添加到你的aes。

#1


1  

Create a new color flag on your dataframe:

在数据框上创建一个新的颜色标记:

gene_list$color_flag <- ifelse(gene_list$logFC > 1.3, 1, ifelse(gene_list$logFC < -1.3, -1, 0))

Then add fill = color_flag to your aes.

然后将fill = color_flag添加到你的aes。