繁体   English   中英

在 r 中使用 ggplot2 在 x 轴上的直方图中指定 bin

[英]Specifying bins in histogram on the x- axis using ggplot2 in r

在 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")

您在正确的轨道上,只需按如下方式修改您的脚本。 您可以尝试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))

在上述示例数据的基础上,该脚本生成以下直方图。 hist_bins

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM