R ggplot:在带有坐标翻转的分组条形图中,条形图的顺序相反
使用中的代码,我意识到一些我不理解的事情:R ggplot:在带有坐标翻转的分组条形图中,条形图的顺序相反,r,ggplot2,R,Ggplot2,使用中的代码,我意识到一些我不理解的事情: library(ggplot2) LoTRdata <- structure(list(Film = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("The Fellowship Of The Ring", "The Return Of The King", "The Two Towers"),
library(ggplot2)
LoTRdata <- structure(list(Film = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("The Fellowship Of The Ring",
"The Return Of The King", "The Two Towers"), class = "factor"),
Race = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L,
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Elf", "Hobbit",
"Man"), class = "factor"), Gender = structure(c(1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("Female", "Male"), class = "factor"), Words = c(1229L,
971L, 14L, 3644L, 0L, 1995L, 331L, 513L, 0L, 2463L, 401L,
3589L, 183L, 510L, 2L, 2673L, 268L, 2459L)), .Names = c("Film",
"Race", "Gender", "Words"), class = "data.frame", row.names = c(NA,
-18L))
p <- ggplot(LoTRdata, aes(x = Race, y = Words, fill = Film))
p + geom_bar(stat = "identity", position = "dodge") +
coord_flip() + guides(fill = guide_legend())
库(ggplot2)
LoTRdatap查看可能的副本;这个问题似乎是相同的,即使有相同的数据。我以前发现过这篇文章,这就是我的问题的来源。因为在最后一个答案中,顺序取决于每组钢筋的最高出现率。然而,我试图找出如何在使用coord_flip
aes(x=Race,y=Words,fill=forcats::fct_rev(Film))
?顺便说一句,发生这种情况的原因是组的闪避增加了x(现在是y),并且(0,0)在左下角。因此,第一组(红色)最接近0,这是最低的。在尝试了大量的解决方法后,这对我来说很有效。
p <- ggplot(LoTRdata, aes(x = Race, y = Words, fill = Film))
p + geom_bar(stat = "identity", position = position_dodge2(reverse=TRUE)) +
coord_flip()