1

我不明白为什么 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 默认为 range/30。使用 'binwidth = x' 来调整它。pmin(y, 0) 中的错误:找不到对象“y”

如果 data.frame 的第一列(等位基因)是一个因素,那么情节就会出来,但是我对数据的排序有问题。我需要列等位基因按顺序排列,如上面的 data.frame 所示。很明显,有什么东西让我无法理解……

4

2 回答 2

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 =适当设置。

重新排序和绘图后,这就是我得到的:

ggplot2_weight

于 2013-02-07T21:25:41.400 回答
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默认为计数(即制作直方图),但如果我理解正确,计数(或值)已经计算出来。

于 2013-02-07T21:28:31.833 回答