【发布时间】:2015-04-27 04:34:12
【问题描述】:
我有一个名为 ret1 的数据框,如下所示:
chr binRight Means
chr1 1.20e+07 25.320212
chr1 2.40e+07 30.875749
chr2 1.60e+07 48.952099
chr2 2.80e+07 41.356140
chr2 6.00e+07 20.078314
chr3 2.20e+07 37.107468
chr3 3.40e+07 38.257566
chr3 9.60e+07 38.381219
chr3 1.08e+08 30.218046
我想创建一个直方图,将 Means 列绘制为按 chr 分组的连续图,以便我可以看到例如每个 binRight 的 chr1 的每个平均值,然后每个 binRight 的 chr2 的每个平均值等在同一轴上.
理想情况下,如果有人知道如何做到这一点,我希望它看起来像一种图形均衡器直方图。
我试过了
p = ggplot(ret1, aes(x=ret1$binRight)) + geom_histogram(ret1$Means) +
p + facet_grid(chr ~ .)
但这给了我每个 chr 的计数和 x 轴上的平均值,虽然我喜欢绘图的外观和 chr 的分离方式,但我需要 x 轴上的 binRight 和 y 轴上的 Means情节。
更新:
根据我所学到的,这应该会为人们提供一些不错的情节:
#Grouping the data across binRight:
library(dplyr)
ret1 <- df %>%
group_by(chr, binnum = (leftPos) %/% 12000000) %>%
summarise(Means = mean(Means)) %>%
mutate(binRight = (binnum+1) * 12000000) %>%
select(binRight, Means)
#Convert chr column to numeric by stripping out the "chr"
ret1$chr<-as.numeric(gsub("chr","",ret1$chr))
#Doing the plot
d<- ggplot(ret1, aes(x=as.numeric(binRight),y=as.numeric(Means)),colour=Means) +
geom_bar(stat = "identity",aes(fill=Means)) +
ggtitle("My plot")+
xlab("Genomic locus") +
ylab("Raw read counts") +
theme(axis.text.y=element_text(size=10)) +
theme(axis.title=element_text(size=14))+
theme(plot.title=element_text(face="bold", size=20)) +
scale_y_continuous(breaks=c(0, 80)) +
facet_grid(chr ~ .)
d +scale_fill_gradient(limits = c(0, 80), low = "green", high = "red").
【问题讨论】: