Coord_flip()在成组的条形图中更改组内条的顺序 [英] Coord_flip() changes ordering of bars within groups in grouped bar plot

查看:460
本文介绍了Coord_flip()在成组的条形图中更改组内条的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个组条形图(img 1),并使用coord_flip()移动了 将条形图分组到y轴.我注意到coord_flip()还重新排列了每个条在组中的显示方式.

I have created a group bar plot (img 1) and used coord_flip() to move the groups bars to the y axis. I noticed that coord_flip() also reorders the way each bar is presented in the groups.

例如在img中,条形图来自A-D组.但是,在img 2中,条形图来自D-A组.如何使用coord_flip()保持每个组中条形的相同顺序?

E.g. In img 1 bars flow from Group A-D. However, in img 2 the bars flow from Group D-A. How can I keep the same ordering of the bars within each group using coord_flip()?

ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) + 
  geom_bar(stat="identity", position="dodge")

ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) + 
  geom_bar(stat="identity", position="dodge") +
  scale_x_discrete(limits = as.character(16:1)) +
  coord_flip()

工作示例(数据子集-问题8-11)

Working example (subset of data -- questions 8-11)

dput() output:

structure(list(group = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), correct_per = c(90.4761904761905, 100, 100, 87.5, 83.3333333333333, 90.9090909090909, 84.6153846153846, 87.5, 80.9523809523809, 88.6363636363636, 100, 70.8333333333333, 63.4146341463415, 76.7441860465116, 76.9230769230769, 62.5), nr_correct = c(38L, 44L, 26L, 21L, 35L, 40L, 22L, 21L, 34L, 39L, 26L, 17L, 26L, 33L, 20L, 15L), nr_incorrect = c(4L, 0L, 0L, 3L, 7L, 4L, 4L, 3L, 8L, 5L, 0L, 7L, 15L, 10L, 6L, 9L), length = c(42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 41L, 43L, 26L, 24L), qid = c("8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11")), .Names = c("group", "correct_per", "nr_correct", "nr_incorrect", "length", "qid"), row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame"))

save to file 

all_Q <- dget(filename)


ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) + 
      geom_bar(stat="identity", position="dodge") +
      scale_x_discrete(limits = as.character(11:8)) +
      coord_flip()

推荐答案

一种"hacky"方法是将position_dodge宽度设置为负数. .9是默认设置,因此-.9会创建相同的外观,但相反.

One "hacky" way to do this is to set the position_dodge width to be a negative number. .9 is the default, so -.9 would create the same appearance but in reverse.

library(ggplot2)

all_Q <- structure(list(group = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), correct_per = c(90.4761904761905, 100, 100,
87.5, 83.3333333333333, 90.9090909090909, 84.6153846153846, 87.5, 80.9523809523809, 88.6363636363636, 100, 70.8333333333333, 63.4146341463415, 76.7441860465116, 76.9230769230769, 62.5), nr_correct = c(38L, 44L, 26L, 21L, 35L, 40L, 22L, 21L, 34L, 39L, 26L, 17L, 26L, 33L, 20L, 15L), nr_incorrect = c(4L, 0L, 0L, 3L, 7L, 4L, 4L, 3L, 8L, 5L, 0L, 7L, 15L, 10L, 6L, 9L), length = c(42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 41L, 43L, 26L, 24L), qid = c("8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11")), .Names = c("group", "correct_per", "nr_correct", "nr_incorrect", "length", "qid"), row.names = c(NA,
-16L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(all_Q, aes(x = qid, y = correct_per, fill = group)) + 
  geom_bar(stat = "identity", position = position_dodge(-.9)) +  
  scale_x_discrete(limits = as.character(11:8)) +
  coord_flip()


或者,您可以重新排列因子的水平,然后反转图例.


Alterantively, you could reorder the levels of the factor, then reverse the legend.

all_Q$group <- factor(all_Q$group, levels = rev(levels(all_Q$group)))

ggplot(all_Q, aes(x = qid, y = correct_per, fill = group)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_x_discrete(limits = as.character(11:8)) +
  coord_flip() + 
  guides(fill = guide_legend(reverse = TRUE))


结果:


Result:

这篇关于Coord_flip()在成组的条形图中更改组内条的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆