我不明白为什么ggplot无法绘制这个data.frame:
sample <- dput(melted)
structure(list(alleles = c(98, 100, 102, 106, 124, 126, 128,
132, 134, 142, 144, 145, 146, 147, 148, 149, 151, 152, 153, 156,
158, 159, 165, 167, 169, 171, 173, 175, 177, 181, 184, 187, 193,
194, 196, 197, 200, 228, 233, 234, 238, 240, 241, 242, 243, 244,
245, 246, 247, 249, 251, 253, 363, 364, 365, 367, 371, 377, 380,
384, 391, 98, 100, 102, 106, 124, 126, 128, 132, 134, 142, 144,
145, 146, 147, 148, 149, 151, 152, 153, 156, 158, 159, 165, 167,
169, 171, 173, 175, 177, 181, 184, 187, 193, 194, 196, 197, 200,
228, 233, 234, 238, 240, 241, 242, 243, 244, 245, 246, 247, 249,
251, 253, 363, 364, 365, 367, 371, 377, 380, 384, 391), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Parents", "Nymphes"
), class = "factor"), value = c(11, 17, 13, 1, 1, 18, 5, 8, 10,
6, 9, 1, 25, 17, 2, 1, 1, 2, 0, 2, 0, 2, 1, 1, 8, 9, 6, 10, 35,
1, 3, 4, 20, 3, 2, 39, 2, 0, 0, 1, 8, 25, 2, 27, 6, 8, 0, 3,
1, 8, 1, 2, 14, 17, 2, 5, 1, 2, 1, 2, 0, 12, 49, 33, 0, 4, 35,
6, 7, 14, 7, 25, 0, 44, 26, 3, 0, 6, 0, 9, 3, 1, 0, 1, 4, 15,
22, 8, 24, 69, 0, 2, 5, 35, 2, 18, 92, 0, 2, 6, 0, 22, 44, 6,
56, 13, 12, 1, 6, 2, 21, 0, 3, 12, 9, 5, 3, 0, 1, 1, 0, 1)), .Names = c("alleles",
"variable", "value"), row.names = c(NA, -122L), class = "data.frame")
ggplot命令:
ggplot(sample,aes(x=alleles,y=value)) + geom_bar(aes(fill=variable),position="dodge") + theme(axis.text.x=element_text(angle = -75, hjust = 0))
错误:
stat_bin:binwidth默认为范围/ 30。使用'binwidth = x'来调整它。 pmin(y,0)出错:找不到对象'y'
如果data.frame(等位基因)的第一列是一个因子,那么该图出来了,但是我的数据排序有问题。我需要列等位基因,如上面的data.frame。显然有些东西逃脱了我......
答案 0 :(得分:1)
我认为您错误地使用了geom_bar
。如果您已计算频率(或条形长度),则应使用weight =
美学,如下所示:
sample$alleles <- factor(sample$alleles)
ggplot(sample, aes(x = alleles)) +
geom_bar(aes(weight = value, fill = variable), position = "dodge") +
theme(axis.text.x=element_text(angle = -75, hjust = 0))
如果通过因子分解没有按照您想要的顺序排列图表,那么您可以这种方式订购您的因子(如果您希望保留数字顺序):
sample$alleles <- factor(sample$alleles,
levels=sample$alleles[!duplicated(sample$alleles)], ordered = T)
现在,绘图应该导致数字顺序。如果您想将订单更改为其他内容,请相应地设置levels =
。
重新排序和绘图后,这就是我得到的:
答案 1 :(得分:1)
如果您正在寻找:
您需要在stat = "identity"
geom_bar
ggplot(sample,aes(x=alleles + (as.numeric (variable)/2 - .75), y=value)) +
geom_bar(aes(fill=variable),position="identity", stat = "identity") +
theme(axis.text.x=element_text(angle = -75, hjust = 0))
geom_bar
默认为计数(即制作直方图),但如果我理解正确,则已计算出计数(或值)。