R 使用ggplot2在直方图背景中添加矩形
我想在使用ggplot2生成的直方图中添加矩形,为背景上色。 不幸的是,我的直方图没有绘制出来,我也不明白为什么。 这是我的代码,谢谢你的帮助:R 使用ggplot2在直方图背景中添加矩形,r,ggplot2,histogram,R,Ggplot2,Histogram,我想在使用ggplot2生成的直方图中添加矩形,为背景上色。 不幸的是,我的直方图没有绘制出来,我也不明白为什么。 这是我的代码,谢谢你的帮助: ##### DATA FOR HISTROGRAM chr = 1:11 nb_SNP = 1000 SNP_positions <- data.frame() for(c in chr){ for(n in 1:nb_SNP){ position <- sample(1:5000000, 1) SNP_positio
##### DATA FOR HISTROGRAM
chr = 1:11
nb_SNP = 1000
SNP_positions <- data.frame()
for(c in chr){
for(n in 1:nb_SNP){
position <- sample(1:5000000, 1)
SNP_positions <- rbind(SNP_positions,data.frame(chr = chr, position = position))
}
}
##### DATA FOR RECTANGLE
df_chr <- data.frame(chr=c(1:11),
xmin = rep(1,11),
xmax = c(40297282, 64237462, 80088348, 41978404, 74731017, 53893726, 52447651, 74330457, 39019482, 39359118, 45510589))
#### MAKE PLOT
MarkerDensity <- ggplot(SNP_positions) +
geom_rect(data = df_chr, aes(xmin = xmin, xmax = xmax, fill = as.factor(chr)),ymin = -Inf, ymax = Inf, alpha = 0.5) +
geom_histogram(aes(x=position), binwidth=1000) +
facet_wrap(~chr,ncol=3) + ggtitle("SNP density over E. grandis genome") +
xlab("Genomic position (1 Kb)") +
ylab("Number of SNPs") +
theme_bw()
plot(MarkerDensity)
历史程序的数据
chr=1:11
nb_SNP=1000
SNP_位置因为矩形阴影x位置与直方图x位置不匹配 下面是一个简化的示例,使用您的SNP数据(为了简单起见,仅针对2条染色体),但使用位置与SNP数据匹配的新矩形数据:
##### DATA FOR RECTANGLE
df_chr <- data.frame(chr = 1:2,
xmin = rep(1,2),
xmax = c(1000, 2000),
ymin = -Inf,
ymax = Inf)
#### MAKE PLOT
ggplot(SNP_positions[ SNP_positions$chr %in% 1:2, ]) +
geom_rect(data = df_chr, aes(xmin = xmin, xmax = xmax, fill = as.factor(chr)),
ymin = -Inf, ymax = Inf, alpha = 0.5) +
geom_histogram(aes(x=position), binwidth=1000) +
facet_wrap(~chr,ncol=2) +
ggtitle("SNP density over E. grandis genome") +
xlab("Genomic position (1 Kb)") +
ylab("Number of SNPs") +
theme_bw()
矩形的数据
df_chr因为矩形阴影x位置与直方图x位置不匹配 下面是一个简化的示例,使用您的SNP数据(为了简单起见,仅针对2条染色体),但使用位置与SNP数据匹配的新矩形数据:
##### DATA FOR RECTANGLE
df_chr <- data.frame(chr = 1:2,
xmin = rep(1,2),
xmax = c(1000, 2000),
ymin = -Inf,
ymax = Inf)
#### MAKE PLOT
ggplot(SNP_positions[ SNP_positions$chr %in% 1:2, ]) +
geom_rect(data = df_chr, aes(xmin = xmin, xmax = xmax, fill = as.factor(chr)),
ymin = -Inf, ymax = Inf, alpha = 0.5) +
geom_histogram(aes(x=position), binwidth=1000) +
facet_wrap(~chr,ncol=2) +
ggtitle("SNP density over E. grandis genome") +
xlab("Genomic position (1 Kb)") +
ylab("Number of SNPs") +
theme_bw()
矩形的数据
df_chr可以使用
聚合
从数据计算xmax
到geom_rect
的参数。然后直方图将匹配矩形宽度
xmax <- aggregate(position ~ chr, SNP_positions, max)[[2]]
##### DATA FOR RECTANGLE
df_chr <- data.frame(chr = 1:10,
xmin = rep(1, 10),
xmax = xmax,
ymin = 0,
ymax = 100000000)
#### MAKE PLOT
MarkerDensity <- ggplot(SNP_positions,aes(x=position)) +
geom_rect(data = df_chr, aes(xmin = xmin, xmax = xmax, fill = as.factor(chr)),ymin = -Inf, ymax = Inf, alpha = 0.5) +
geom_histogram(binwidth = 50) +
facet_wrap(~chr,ncol=3) + ggtitle("SNP density over E. grandis genome") +
xlab("Genomic position (1 Kb)") +
ylab("Number of SNPs") +
theme_bw() +
theme(axis.text.x = element_text(angle = 60, hjust=1))
MarkerDensity
xmax可以使用aggregate
从数据计算geom\u rect
的xmax
参数。然后直方图将匹配矩形宽度
xmax <- aggregate(position ~ chr, SNP_positions, max)[[2]]
##### DATA FOR RECTANGLE
df_chr <- data.frame(chr = 1:10,
xmin = rep(1, 10),
xmax = xmax,
ymin = 0,
ymax = 100000000)
#### MAKE PLOT
MarkerDensity <- ggplot(SNP_positions,aes(x=position)) +
geom_rect(data = df_chr, aes(xmin = xmin, xmax = xmax, fill = as.factor(chr)),ymin = -Inf, ymax = Inf, alpha = 0.5) +
geom_histogram(binwidth = 50) +
facet_wrap(~chr,ncol=3) + ggtitle("SNP density over E. grandis genome") +
xlab("Genomic position (1 Kb)") +
ylab("Number of SNPs") +
theme_bw() +
theme(axis.text.x = element_text(angle = 60, hjust=1))
MarkerDensity
xmax