多栏ggplot2图表中的订购图例

时间:2018-12-06 18:37:22

标签: r ggplot2 geom-bar

我想要一个多条形图,横条,其中条形颜色的图例与条形本身的顺序相同。我看过几篇文章,对这里的示例产生了影响,但我无法使其正常工作。

在此示例中,“组”在条形图中是一个顺序,在图例中是相反的顺序。

library(ggplot2)

# create data for tidy format
Category <- c("Category1","Category2","Category3","Category4","Category5","Category1","Category2","Category3","Category4","Category5","Category1","Category2","Category3","Category4","Category5")
GroupTitle <- c("GroupA","GroupA","GroupA","GroupA","GroupA","GroupB","GroupB","GroupB","GroupB","GroupB","GroupC","GroupC","GroupC","GroupC","GroupC")
Valuelst <- list()
for (i in 1:15){
  Valuelst[i] <- runif(1, min=0, max=1)
}
Valuelst <- unlist(Valuelst)
# make data frame
d <- data.frame(Category,GroupTitle,Valuelst)

# set factors and orders desired
d$Category <- factor(d$Category, levels = c("Category5","Category4","Category3","Category2","Category1"))
d$GroupTitle <- factor(d$GroupTitle, levels = c("GroupA","GroupB","GroupC"))

# make graph
ggplot(d, aes(x=Category, y=Valuelst, order = -as.numeric(GroupTitle))) + # order= -as.numeric() is one solution that I read
  geom_bar(aes(fill=GroupTitle), stat="identity", position="dodge") + 
  coord_flip() +
  scale_fill_manual("Legenda", values = c("GroupC" = "#deebf7", "GroupB" = "#3182bd", "GroupA" = "#9ecae1")) # scale_fill_manual is another I read

Output of the code

我在代码中评论了一些尝试其他地方看到的解决方案的地方。即,我已经确保组是一个因素,设置了该因素的顺序,使用了order = -as.numeric(GroupTitle),并尝试了scale_fill_manual,所有这些都没有任何作用。

3 个答案:

答案 0 :(得分:1)

选项1-顺序:C,B和A

要反转图例标签,您只需添加:guides(fill = guide_legend(reverse = TRUE))。保持条形的原始顺序:C,B和A。

ggplot(d, aes(x = Category,
              y = Valuelst,
              fill = GroupTitle)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  scale_fill_manual("Legenda",
                    values = c(
                      "GroupC" = "#deebf7",
                      "GroupB" = "#3182bd",
                      "GroupA" = "#9ecae1"
                    )) +
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

选项2-顺序:A,B和C

为了反转条形图的顺序,我们只是在绘制之前对级别进行重新排序。

d$GroupTitle <- factor(d$GroupTitle, levels = c("GroupC","GroupB","GroupA"))

# make graph
ggplot(d, aes(x = Category,
              y = Valuelst,
              fill = GroupTitle)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  scale_fill_manual("Legenda",
                    values = c(
                      "GroupC" = "#deebf7",
                      "GroupB" = "#3182bd",
                      "GroupA" = "#9ecae1"
                    )) +
  guides(fill=guide_legend(reverse=TRUE))

enter image description here

答案 1 :(得分:0)

我使用了ggpubr库。诀窍是-0.7的{​​{1}},使条形反转。您也可以使用

position_dodge()

enter image description here

您还可以在library(ggplot2) library(ggpubr) # create data for tidy format Category <- paste0("Category", rep(1:5,3)) GroupTitle <- paste0("Group", rep(LETTERS[1:3], each=5)) Valuelst <- runif(15, min=0, max=1) # make data frame - factor is default for strings d <- data.frame(Category, GroupTitle, Valuelst) # make graph ggbarplot(d,x="Category", y="Valuelst", fill="GroupTitle", legend = "right", orientation = "horiz", position=position_dodge(-.7), order = c("Category5","Category4","Category3","Category2","Category1")) + scale_fill_manual("Legenda", values = c("GroupC" = "#deebf7", "GroupB" = "#3182bd", "GroupA" = "#9ecae1")) 中使用position=position_dodge(-.9)来达到相同的效果。

geom_bar()

答案 2 :(得分:0)

您几乎和scale_fill_manual在一起。

  scale_fill_manual("Legenda", 
                    values = c("GroupA" = "#9ecae1", "GroupB" = "#3182bd", "GroupC" = "#deebf7"),
                    breaks = c("GroupC", "GroupB", "GroupA")) 

breaks参数将设置顺序。

Ordered Legend

相关问题