gpt4 book ai didi

r - 使用 ggplot 生成曼哈顿图

转载 作者:行者123 更新时间:2023-12-04 14:04:44 25 4
gpt4 key购买 nike

我一直在尝试使用 ggplot 生成曼哈顿图,我终于开始工作了。然而,尽管我尝试了几个我在网上看到的不同例子,但我无法得到要被染色体着色的点。我附上了我的代码和下面的结果图。谁能看出为什么代码无法按染色体对点进行着色?

library(tidyverse)
library(vroom)

# threshold to drop really small -log10 p values so I don't have to plot millions of uninformative points. Just setting to 0 since I'm running for a small subset
min_p <- 0.0

# reading in data to brassica_df2, converting to data frame, removing characters from AvsDD p value column, converting to numeric, filtering by AvsDD (p value)
brassica_df2 <- vroom("manhattan_practice_data.txt", col_names = c("chromosome", "position", "num_SNPs", "prop_SNPs_coverage", "min_coverage", "AvsDD", "AvsWD", "DDvsWD"))
brassica_df2 <- as.data.frame(brassica_df2)
brassica_df2$AvsDD <- gsub("1:2=","",as.character(brassica_df2$AvsDD))
brassica_df2$AvsDD <- as.numeric(brassica_df2$AvsDD)
brassica_df2 <- filter(brassica_df2, AvsDD > min_p)

# setting significance threshhold
sig_cut <- -log10(1)

# settin ylim for graph
ylim <- (max(brassica_df2$AvsDD) + 2)

# setting up labels for x axis
axisdf <- as.data.frame(brassica_df2 %>% group_by(chromosome) %>% summarize(center=( max(position) + min(position) ) / 2 ))

# making manhattan plot of statistically significant SNP shifts
manhplot <- ggplot(data = filter(brassica_df2, AvsDD > sig_cut), aes(x=position, y=AvsDD), color=as.factor(chromosome)) +
geom_point(alpha = 0.8) +
scale_x_continuous(label = axisdf$chromosome, breaks= axisdf$center) +
scale_color_manual(values = rep(c("#276FBF", "#183059"), unique(length(axisdf$chromosome)))) +
geom_hline(yintercept = sig_cut, lty = 2) +
ylab("-log10 p value") +
ylim(c(0,ylim)) +
theme_classic() +
theme(legend.position = "n")
print(manhplot)

enter image description here

最佳答案

我认为您只需要在对 aes() 的调用中移动您的 color=... 参数:

ggplot(
data = filter(brassica_df2, AvsDD > sig_cut),
aes(x=position, y=AvsDD),
color=as.factor(chromosome))

变成...

ggplot(
data = filter(brassica_df2, AvsDD > sig_cut),
aes(x=position, y=AvsDD, color=as.factor(chromosome)))

关于r - 使用 ggplot 生成曼哈顿图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68926957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com