gpt4 book ai didi

r - 为什么我的 ggplot2 条形图不显示大于 0 的 ylim 最小值?

转载 作者:行者123 更新时间:2023-12-04 09:35:54 24 4
gpt4 key购买 nike

我正在尝试按 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)

最佳答案

您只需设置 oob = rescale_none 参数,它就会起作用:

library(scales)
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),
oob = rescale_none)

enter image description here

确保为 oob = rescale_none 附加 scales 包。

关于r - 为什么我的 ggplot2 条形图不显示大于 0 的 ylim 最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32617914/

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