首页 > 解决方案 > 如何在ggplot中为火山图添加颜色?

问题描述

我想更改图表中点的颜色,但是我创建的矢量没有这样做。我所有的代码似乎都运行良好,但图表不会按我想要的方式打印。

左侧应为蓝色,右侧应为红色。

# Basic Volcano Plot 
VP <- ggplot(df.7vsNO, aes(x = log2FC, y = logpv)) + geom_point() + theme_minimal()
print(VP)

VP2 <- VP + geom_vline(xintercept=c(-1.6, 1.6), col="red") +
    geom_hline(yintercept=-log10(0.05), col="red")

print(VP2)
# add a column of NAs
df.7vsNO$diffexpressed <- "NO"

# if log2FoldChange > 1, set as "UP"
df.7vsNO$diffexpressed[df.7vsNO$log2FC > 1] <- "UP"

#if log2FoldChange < 1, set as "DOWN"
df.7vsNO$diffexpressed[df.7vsNO$log2FC < 1] <- "DOWN"
# Re-plot but this time time color the points w/ "diffexpressed"

VP <- ggplot(df.7vsNO, aes(x = log2FC, y = logpv, col(diffexpressed))) + geom_point() + theme_minimal()

print(VP)

# Add lines as before...

VP2 <- VP + geom_vline(xintercept=c(-1.6, 1.6), col="red") +
    geom_hline(yintercept=-log10(0.05), col="red")

print(VP2)
# Change point color

VP3 <- VP2 + scale_color_manual(values=c("blue", "black", "red"))

# named vector

mycolors <- c("blue", "red", "black")
names(mycolors) <- c("DOWN", "UP", "NO")

VP3 <- VP2 + scale_color_manual(values = mycolors)

print(VP3)

标签: rggplot2

解决方案


我会根据 logFC2 的值创建一个新变量,并从那里为它着色。

library(dplyr)
df.7vsNO %>%
mutate(colour.grp = ifelse(logGC > 1.6, yes = "above", no = "nothing")) %>%
mutate(colour.grp = ifelse(logGC < -1.6, yes = "below", no = colour.grp))

其次是

geom_point(aes(colour = colour.grp)) +
scale_colour_manual(values = c("green", "red", "grey")

不记得如何订购它们。


推荐阅读