Reputation: 1572
I don't understand why ggplot can't plot this 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 command :
ggplot(sample,aes(x=alleles,y=value)) + geom_bar(aes(fill=variable),position="dodge") + theme(axis.text.x=element_text(angle = -75, hjust = 0))
error :
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this. Error in pmin(y, 0) : object 'y' not found
If first column of data.frame (alleles) is a factor, then the plot comes out, but then I have problems with the ordering of data. I need column alleles to be in order, as in the above data.frame. There is obviously something that escapes me...
Upvotes: 1
Views: 1424
Reputation: 14093
If this is what you are looking for:
you need to set stat = "identity"
in 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
defaults to counting (i.e. makeing a histogram), but if I understood you correctly, the counts (or values) are already calculated.
Upvotes: 1
Reputation: 118889
I think you're using geom_bar
wrongly. If you already have the frequency (or length of the bar) calculated, you should use the weight =
aesthetic as follows:
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))
If by factoring it doesn't order the plot in the order you desire, then you can order your factor in this manner (if you want the numerical order preserved):
sample$alleles <- factor(sample$alleles,
levels=sample$alleles[!duplicated(sample$alleles)], ordered = T)
Now, plotting should result in the numerical order. If you want to change the order to something else, set the levels =
appropriately.
After reordering and plotting, this is what I get:
Upvotes: 1