我创建了一个组条形图(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, …Run Code Online (Sandbox Code Playgroud)