【问题标题】:stat_summary_bin at x-axis ticksx 轴刻度处的 stat_summary_bin
【发布时间】:2017-11-28 22:32:02
【问题描述】:

如何让 ggplot 的 stat_summary_bin 在指定的 x 轴值处进行汇总?例如,在下面的图中,我想要 x=-1、0、1、2 等处的摘要。现在是 -1.5、-0.5、0.5 和 1.5。

df = data.frame(x=rnorm(1000), y=rnorm(1000))
ggplot(df, aes(x=x, y=y)) +
stat_summary_bin(binwidth=1)

【问题讨论】:

  • 也许可以定义breaks?比如breaks = seq(-4.5, 4.5, 1) 什么的。
  • @aosmith 在哪里? stat_summary_bin 没有参数 break 也不关心 scale_x_continuous(breaks=-10:10)
  • 在我使用的 ggplot2 版本 (ggplot2_2.2.1.9000) 中,stat_summary_bin 中有一个 breaks 参数。文档中对此进行了解释。
  • @aosmith Breaks 选项仅添加到July 28, 2017. 上的ggplot2 的开发版本中,看来您正在运行最前沿的代码,这很好,但大多数人使用的包克兰。

标签: r ggplot2 binning


【解决方案1】:

这是 ggplot2 中的一个已知错误,仅在 late July 中修复。一种解决方法,来自错误报告:

df <- data.frame(x = rnorm(1000), y = rnorm(1000))
ggplot(df, aes(x = cut_width(x, 1, center = 0), y = y)) +
  stat_summary_bin(binwidth = 1)

并且,固定 x 轴:

ggplot(df, aes(x = cut_width(x, 1, center = 0), y = y)) +
  stat_summary_bin(binwidth = 1) +
  scale_x_discrete(labels = -3:3, name = 'x')

breaks 参数将在 ggplot2 的下一个版本中提供,该版本应该会在接下来的几周内发布。

如果你现在想运行 ggplot2 的开发版本,你应该可以通过:

devtools::install_github("tidyverse/ggplot2")

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多