gpt4 book ai didi

r - Coord_flip() 更改分组条形图中组内条形的顺序

转载 作者:行者123 更新时间:2023-12-04 10:40:11 24 4
gpt4 key购买 nike

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

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

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

enter image description here
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()

enter image description here

工作示例(数据子集——问题 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将创建相同的外观,但相反。

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()

或者,您可以重新排序因子的级别,然后反转图例。
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))

结果:

enter image description here

关于r - Coord_flip() 更改分组条形图中组内条形的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49448497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com