【问题标题】:Display x tick values for all bins in ggplot in R在 R 中显示 ggplot 中所有 bin 的 x 刻度值
【发布时间】:2021-03-05 04:10:25
【问题描述】:

我有一个包含收入列的数据框,我正在尝试使用 ggplot 显示直方图。我需要在 40000(最小值为 40003)处开始直方图,并在 400000 处结束直方图,宽度为 20000。 下面是我编写的代码,它显示了直方图。但我也想显示所有箱子的 x 刻度值。例如,在图中,bin 的 x 轴值应为 40000、60000、80000 等。下面是我的代码

ggplot(boston, aes(x=Earnings)) + 
  geom_histogram(breaks=seq(40000,400000,by=20000),binwidth = 20000, fill="#69b3a2", color="#e9ecef", alpha=1)

非常感谢社区的帮助。

【问题讨论】:

  • 试试scale_x_continuous(breaks=seq(40000,400000,by=20000))

标签: r ggplot2


【解决方案1】:

如果你想包含 bin 标签,使用这个:

ggplot(boston, aes(x=Earnings)) +
  geom_histogram(binwidth = 20000, fill="#69b3a2", color="#e9ecef") +
  scale_x_continuous(name="Earnings (thousands)",limits=c(40000, 400000),
                     breaks=seq(40000,400000,by=20000),
                     labels=number_format(accuracy=1, scale=1/1000)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 

标签缩放是可选的。你也可以改变文字的角度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多