【发布时间】:2018-09-01 23:07:26
【问题描述】:
在下面显示的图中,我试图更改 y 轴上标签的顺序(coord_flip() 之前的 x 轴)。 我希望 1 位于顶部,16 位于底部。
levels <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
library(ggplot2)
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group), group=qid) +
geom_bar(stat="identity", position=position_dodge()) +
scale_x_discrete(name = "Questions", limits = levels) +
scale_y_continuous(name = "Percent correct") +
coord_flip()
到目前为止,这是我尝试过的:
levels <- c(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) -> 没有变化。
limits = rev(levels) -> 没有变化
limits = rev(levels(levels)) -> 来自this question/answer img 下面的建议
包含问题子集的可重现示例 (8,9,10,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)
levels <- c(8,9,10,11)
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group), group=qid) +
geom_bar(stat="identity", position=position_dodge()) +
scale_x_discrete(name = "Questions", limits = levels) +
scale_y_continuous(name = "Percent correct") +
coord_flip()
【问题讨论】:
-
请用
dput展示一个可重复的小例子