r - 如何在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)
解决方案
我会根据 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")
不记得如何订购它们。
推荐阅读
- javascript - 将对象从 API 存储到状态
- c++ - 我在解决此错误时遇到问题。我正在努力在主函数中传递数组
- azure-devops - 如何从 azure devops 服务器将 Windows 服务部署到 onprem vm?
- angular - 在 angular2+ 处包含锚链接或在 (event.url) 中添加多个 url
- angular - 令牌过期后在 signalR 中更新令牌
- java - 如何在 rest akka-http 上重写 POST 方法
- c++ - C/C++:查找默认网络接口向上/向下的 POSIX 兼容方式
- google-chrome-extension - 具有新选项卡覆盖扩展的 chrome 顶级站点
- css - 如何在 vue-vuetify 项目中使用自定义谷歌字体类?
- javascript - 如何在 Chrome 扩展中访问 CORS graphql?我修改了标题但没有工作