【问题标题】:Histogram not showing all x-axis labels in R直方图未显示 R 中的所有 x 轴标签
【发布时间】:2020-07-05 17:00:59
【问题描述】:

我绘制了一个直方图,但由于某种原因,x 标签是部分的。

我希望每年都写在 x 轴上。这是我的代码:

ggplot(video_games, aes(x = Year)) + geom_histogram(stat = 'count') + lims(x = c(1995, 2017))

【问题讨论】:

  • ggplot(video_games, aes(x = factor(Year))) + geom_histogram(stat = 'count') 应该有帮助。
  • @Duck 给出错误,不是正确的解决方案
  • 所以请dput(video_games) 包含在您的问题中,以便为您提供帮助。

标签: r ggplot2 histogram


【解决方案1】:

尝试使用breaks 参数添加scale_x_continuous

ggplot(...) +
  ... +
  scale_x_continuous(breaks = 1995:2016)

【讨论】:

  • 不,它删除了所有标签
  • 如果标签消失,这意味着您的 Year 变量被视为连续而不是离散的。查看更新的答案 - 使用 scale_x_continuous 而不是 scale_x_discrete 应该可以解决问题。
【解决方案2】:

你可以试试:

ggplot(video_games, aes(x = year)) + 
  geom_bar() +
  scale_x_continuous(breaks = unique(video_games$year)) +
  coord_flip()

coord_flip 存在是因为您将在 x 轴上有很多“长”标签。如果不想翻转可以旋转:

theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    相关资源
    最近更新 更多