我创建了一个组条形图(img 1)并用于coord_flip()将组条形移动到 y 轴。我注意到coord_flip()它还重新排序了每个条在组中的显示方式。
例如,在 img 1 条中,从 AD 组流出。但是,在 img 2 中,条形图来自 DA 组。如何使用 保持每个组中条形的相同顺序coord_flip()?
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) +
geom_bar(stat="identity", position="dodge")
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
工作示例(数据子集——问题 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()
Run Code Online (Sandbox Code Playgroud)
一种“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()
Run Code Online (Sandbox Code Playgroud)
或者,您可以重新排序因子的级别,然后反转图例。
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))
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
2467 次 |
| 最近记录: |