【问题标题】:Adaptive settings for ggplot?ggplot的自适应设置?
【发布时间】: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) 会满足您的要求吗?

标签: r ggplot2


【解决方案1】:

这是一个笨重的变体,通过计算最小值和最大值,然后控制ylim 内的coord_flip

library(tidyverse)
data.frame(stringsAsFactors=FALSE, 
       ID = c("Skole", "Land", "Skole", "Land", "Skole", "Land"),
       value = c(0.654590909090909, 0.528446335193598, 0.631238336316461,
                 0.550262048394226, 0.69981060606061, 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)"))) %>% 
  mutate(low = min(value) - min(value) * 0.05,
     high = max(value) + max(value) * 0.05) -> example1 

example1 %>% 
  ggplot(aes(variable, 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")) +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme(legend.title=element_blank()) +
  coord_flip(ylim=c(mean(test$low), mean(test$high)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多