【问题标题】:Specifying bins in histogram on the x- axis using ggplot2 in r在 r 中使用 ggplot2 在 x 轴上的直方图中指定 bin
【发布时间】:2019-08-06 16:17:49
【问题描述】:

在 r 中绘制直方图时,我想指定我的 bin,但我得到了一些其他的。

这是我的长度数据

pss <-
structure(list(LengthSize = c(48, 39, 94, 30, 81, 49, 44, 85, 
44, 55, 45, 47, 44, 43, 42, 44, 76, 42, 65, 43, 43, 90, 105, 
32, 31, 43, 36, 65, 21, 15, 113, 113, 44, 46, 94, 90, 95, 37, 
25, 72, 49, 46, 48, 49, 49, 44, 50, 48, 37, 37, 55, 60, 65, 30, 
22, 26, 43, 43, 43, 43, 18, 67, 110, 64, 28, 29, 38, 37, 38, 
37, 38, 70, 58, 65, 55, 60, 40, 22, 68, 88, 88, 32, 44, 86, 37, 
38, 67, 52, 48, 123, 50, 114, 37, 38, 39, 41, 60, 55, 50, 99, 
57, 44, 45, 45, 51, 44, 45, 37, 39, 43, 43, 50, 51, 34, 42, 44, 
46, 67, 67, 56, 56, 57, 56, 47, 65, 66, 43, 41, 69, 45, 114, 
60, 55, 37, 88, 85, 39, 39, 46, 50, 60, 44, 77, 61, 68, 46, 114, 
51, 105, 48, 95, 32, 40, 28, 42, 47, 46, 48, 50, 96, 45, 47, 
118, 55, 60, 34, 118, 39, 52, 119, 40, 55, 60, 55, 59, 102, 73, 
42, 78, 56, 74, 102, 88, 38, 36, 33, 34, 41, 120, 50, 46, 79, 
98, 65, 40, 45, 42, 50, 61, 44)), 
row.names = c(NA, 200L), class = "data.frame")

这是我在 ggplot2 中的代码。

pss %>% 
ggplot(data = pss,breaks = 25,xlim = c(0,528,11), mapping = aes(x = LengthSize )) +
    geom_histogram(binwidth = 10, col = "black", fill = "grey")

【问题讨论】:

  • 请登录并查看您问题中的数据。眼睛不是很舒服。请适当地格式化您的帖子。最好使用dput 提供数据。使用dput(head(df,n))
  • 只需在您的geom_histogram(binwidth = 10, bins=50, col = "black", fill = "grey") 中传递bins=no_bins_you_want。尝试不同的数字,直到找到符合您需求的数字。
  • 您不需要管道 pss %&gt;%ggplot(data = pss, etc)。只使用其中之一。 (建议:不是管道。)

标签: r


【解决方案1】:

您在正确的轨道上,只需按如下方式修改您的脚本。您可以尝试使用binwidthbins。如果您不想要它们,您可以轻松地修改或删除 titlexlabylab

library(ggplot2)
ggplot(pss, mapping = aes(LengthSize)) + geom_histogram(binwidth = 3, bins=50, col = "black", fill = "grey") + ggtitle("My plot title") + xlab("My X axis label - length size") + ylab("Y axis label - Fequency count") + theme(plot.title = element_text(hjust = 0.5))

根据上面的示例数据,脚本生成以下直方图。

【讨论】:

  • Deepseefan 我正在尝试你的代码,但得到的情节与我一直在生产的相同,我的垃圾箱是 100、200 等等
  • @RobertWilson,看看here,如果有帮助,请告诉我。
  • @Deepseefan 我将小数视为我的垃圾箱,而不是实际的垃圾箱,所以它没有帮助。
  • @RobertWilson 你能编辑你的帖子并附上你的输出吗?
  • 上图代表我的数据,但在我运行它时并没有反映在我的代码中。
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
相关资源
最近更新 更多