【发布时间】:2019-01-07 09:38:04
【问题描述】:
我正在编写一个脚本,为参与测试/调查的每所学校生成一份统计报告。
为此,我希望能够根据每所学校的具体数据自动调整图表中 x 轴和 y 轴的限制。
例如,我希望将百分比范围显示为低于可用特定数据的 5% 和高于 5% 的可用数据。
例如,我有以下图表:
library(ggplot2)
library(scales)
example1 <- data.frame(stringsAsFactors=FALSE,
ID = c("Skole", "Land", "Skole", "Land", "Skole", "Land"),
value = c(0.654590909090909, 0.528446335193598, 0.631238336316461,
0.550262048394226, 0.669981060606061, 0.502430105282051),
variable = as.factor(c("Measurement and Geometry(Applying)",
"Measurement and Geometry(Applying)",
"Measurement and Geometry(Knowing)",
"Measurement and Geometry(Knowing)",
"Measurement and Geometry(Reasoning)",
"Measurement and Geometry(Reasoning)")))
item <- ggplot(example1, aes(x=variable, y=value, fill=ID)) +
geom_bar(stat="identity", position = position_dodge()) +
labs (y = "Procentdel rigtige besvarelser", title = "Matematik", x="Fagområde") +
scale_fill_manual(values=c("grey","blue")) +
coord_flip() +
guides(fill = guide_legend(reverse = TRUE)) +
theme(legend.title = element_blank()) +
scale_y_continuous(labels = percent)
item
以下是示例图片:
谢谢!
【问题讨论】:
-
您能添加一张显示示例图的图片吗?
-
我在图片中添加了一个链接,显示了与代码对应的示例:)。
-
您可以尝试通过
scale_y_continuous函数的breaks参数传递一个更改范围的自定义函数。或者试试expand参数。 -
如果您的问题是当您指定限制时条形消失,请查看this question
-
在
scale_y_continuous中设置expand = c(0, 0.05)会满足您的要求吗?