使用ggplot绘制熔化的data.frame,aes(x)是否需要作为一个因子?

使用ggplot绘制熔化的data.frame,aes(x)是否需要作为一个因子?,r,ggplot2,R,Ggplot2,我不明白为什么ggplot无法绘制此数据。帧: 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, 1

我不明白为什么ggplot无法绘制此数据。帧:

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")
错误:

stat_bin:bin宽度默认为范围/30。使用“binwidth=x”调整此值。 pmin(y,0)中出错:未找到对象“y”


如果data.frame的第一列(等位基因)是一个因子,那么图就出来了,但是我对数据的顺序有问题。我需要列等位基因有序排列,如上面的data.frame。很明显,有些东西我不明白……

我认为你使用的
geom\u-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$等位基因如果这是您正在寻找的:

您需要在
geom\u栏中设置
stat=“identity”

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
默认为计数(即绘制直方图),但如果我理解正确,计数(或值)已经计算出来。

@arun:不,如果仔细看,x轴是连续的(对于系数,每个级别都有相同的宽度,除非创建空级别,否则中间没有间隙)@Arun:不一定:
x=allegenes+(as.numeric(variable)/2-.75)
position=“identity”
对我有帮助。我会更新帖子的。
sample$alleles <- factor(sample$alleles, 
                levels=sample$alleles[!duplicated(sample$alleles)], ordered = T)
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))