ggplot2(v2.0.0)似乎不尊重因子顺序。
##################
> dput(temp)
structure(list(phylo_sig = c(0.148740270638472, 0.148740270638472,
0.148740270638472, 0.148740270638472, 0.148740270638472), trait = c("p_corrected_percent",
"p_corrected_percent", "p_corrected_percent", "p_corrected_percent",
"p_corrected_percent"), value = c(0.031015586683513, 0.0553330412480036,
0.73674222087599, 0.114517508485538, 0.0623916427069555), varlevel = structure(c(3L,
2L, 4L, 5L, 1L), .Label = c("species", "genus", "family", "plot_code",
"Residual"), class = "factor")), .Names = c("phylo_sig", "trait",
"value", "varlevel"), row.names = c(28L, 62L, 96L, 130L, 164L
), class = "data.frame")
> str(temp)
'data.frame': 5 obs. of 4 variables:
$ phylo_sig: num 0.149 0.149 0.149 0.149 0.149
$ trait : chr "p_corrected_percent" "p_corrected_percent" "p_corrected_percent" "p_corrected_percent" ...
$ value : num 0.031 0.0553 0.7367 0.1145 0.0624
$ varlevel : Factor w/ 5 levels "species","genus",..: 3 2 4 5 1
> ggplot(temp, aes(x=trait, y = value)) + geom_bar(stat = "identity", aes(fill=varlevel, order = varlevel))
##################
上面的代码会产生正确的图例顺序,但堆叠顺序不正确。 我必须更改实际数据帧行的顺序,以强制堆叠工作:
##################
> temp = temp[c(5,2,1,3,4),]
> ggplot(temp, aes(x=trait, y = value)) + geom_bar(stat = "identity", aes(fill=varlevel, order = varlevel))
##################
这导致正确的堆叠。 任何协助让ggplot在堆叠条形图时注意因素排序都会非常感激!
谢谢, 艾莉