【发布时间】:2015-12-13 14:09:29
【问题描述】:
我试图在 1 到 5 的范围内绘制答案,我希望我在 ggplot2 中的绘图范围从 1 到 5。但是,当我更改 scale_y_continuous(limits = c(1, 5)) 时,数据消失了。任何想法如何解决这个问题(除了从我的值中减去 1 并重新标记的 hack-y 方式)?
可重现的例子:
dat <- structure(list(year = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L), .Label = c("2011", "2012", "2013", "2015"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), .Label = c("instructor.knowledge",
"instructor.enthusiastic", "instructor.clear", "instructor.prepared",
"instructor.feedback", "instructor.out.of.class", "class.dynamic"
), class = "factor"), value = c(5, 4.75, 5, 4.75, 5, 5, 4.85714285714286,
4.75, 4.75, 4.75, 4.71428571428571, 3.75, 5, 4.75, 5, 4.5, 5,
4.75, NA, 5, 5, 5, NA, 4.5, 5, 5, NA, 4.5)), row.names = c(NA,
-28L), .Names = c("year", "variable", "value"), class = "data.frame")
library(ggplot2)
ggplot(dat, aes(x = variable, y = value, fill = year)) +
geom_bar(position = "dodge", stat = "identity") +
scale_y_continuous(name = "Average score across all respondents",
limits = c(1, 5), # fails
# limits = c(0, 5), # succeeds
breaks = 1:5)
【问题讨论】:
-
什么意思?您将 (0,1) 排除在外。整个数据都消失了?
-
因为条形图从 0 开始,它们不能在绘图限制之外开始。你可以做
+coord_cartesian(ylim=c(1,5))