我怎样才能用ggplot2制作堆叠的barplot [英] how can I make stacked barplot with ggplot2
问题描述
你好,我想用ggplot2制作一个堆叠的barplot,下面的数据是
pre $ ch $ NonSyn_Snps Total_exonic_Snps
A01 9217 13725
A02 6226 9133
A03 14888 21531
A04 5272 7482
A05 4489 6608
A06 8298 12212
A07 6351 9368
A08 3737 5592
A09 12429 18119
A10 7165 10525
基本上我想堆叠NonSyn_Snps和Total_exonic_Snps对于每一条染色体,但不幸的是我不能。
这就是我迄今为止没有运气所尝试的结果
ggplot (Chr.df_mod,aes(Chr,Total_exonic_Snps,fill = NonSyn_Snps))+ geom_bar(stat =identity,color =white)+ xlab(染色体)+ ylab(SNP数量)
我得到的是情节,但没有堆积一个。
有人可以帮我吗解决这个问题。
感谢
Upendra
ggplot
成语对于长数据而非宽数据效果最佳。您需要将您的宽数据帧融入长格式,才能从ggplot的许多选项中受益。
#get data
dat< - read.table(text =Chr NonSyn_Snps Total_exonic_Snps
A01 9217 13725
A02 6226 9133
A03 14888 21531
A04 5272 7482
A05 4489 6608
A06 8298 12212
A07 6351 9368
A08 3737 5592
A09 12429 18119
A10 7165 10525,header = TRUE)
$加载库
require(ggplot2)
require(reshape2)
#将数据从宽到长熔化
dat_m< - 熔化(dat)
$ plot
ggplot(dat_m,aes(Chr,value,fill = variable))+
geom_bar(stat =identity)+
xlab(Chromosome )+
ylab(SNP数量)
Hi I wanted to make a stacked barplot using ggplot2 with below data
Chr NonSyn_Snps Total_exonic_Snps
A01 9217 13725
A02 6226 9133
A03 14888 21531
A04 5272 7482
A05 4489 6608
A06 8298 12212
A07 6351 9368
A08 3737 5592
A09 12429 18119
A10 7165 10525
Basically i want to stack NonSyn_Snps and Total_exonic_Snps for each chromosome but unfortunately i cannot.
This is what i tried so far with no luck
ggplot(Chr.df_mod, aes(Chr, Total_exonic_Snps, fill = NonSyn_Snps)) + geom_bar(stat = "identity", colour = "white") + xlab("Chromosome") + ylab("Number of SNPs")
I am getting plot but not stacked one.
Can someone please help me troubleshoot this.
Thanks Upendra
The ggplot
idiom works best with long data rather than wide data. You need to melt your wide data frame into long format to benefit from many of ggplot's options.
# get data
dat <- read.table(text = "Chr NonSyn_Snps Total_exonic_Snps
A01 9217 13725
A02 6226 9133
A03 14888 21531
A04 5272 7482
A05 4489 6608
A06 8298 12212
A07 6351 9368
A08 3737 5592
A09 12429 18119
A10 7165 10525", header= TRUE)
# load libraries
require(ggplot2)
require(reshape2)
# melt data from wide to long
dat_m <- melt(dat)
# plot
ggplot(dat_m, aes(Chr, value, fill = variable)) +
geom_bar(stat = "identity") +
xlab("Chromosome") +
ylab("Number of SNPs")
这篇关于我怎样才能用ggplot2制作堆叠的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!