-2

In am plotting histogram in r an I want to specify my bins but I am getting some else.

This is my lenght data

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

This is my code in ggplot2.

pss %>% 
ggplot(data = pss,breaks = 25,xlim = c(0,528,11), mapping = aes(x = LengthSize )) +
    geom_histogram(binwidth = 10, col = "black", fill = "grey")
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Please log in and take a look at the data in your question. It is not very pleasant to the eyes. Please format your post appropriately. It's also better to provide data with `dput`. Use `dput(head(df,n))`. – NelsonGon Aug 06 '19 at 16:27
  • 1
    Just pass `bins=no_bins_you_want` inside your `geom_histogram(binwidth = 10, bins=50, col = "black", fill = "grey")`. Experiment with different numbers till you find the one appealing to your desire. – deepseefan Aug 06 '19 at 16:36
  • You don't need the pipe `pss %>%` and then `ggplot(data = pss, etc)`. Use just one of them. (Suggestion: not the pipe.) – Rui Barradas Aug 06 '19 at 16:44

1 Answers1

0

You're in the right track, just modify your script as follows. You can experiment with binwidth and bins. You can easily modify or remove the title, xlab and ylab if you don't want them.

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

On the basis of the sample data above, the script produces the following histogram. hist_bins

deepseefan
  • 3,701
  • 3
  • 18
  • 31