Presentation of data in ggplot2

4

Hello, I'm having problems with ggplot2 in a histogram.

The code:

ggplot(data.combined[1:891,], aes(x = Age, fill=Survived)) + 
  facet_wrap(~Sex + Pclass) + 
  geom_histogram(binwidth = 10) + 
  xlab ("Age") + 
  ylab ("Total Count")

With the following code, using exactly the same data as a tutorial I'm getting a histogram where the value of 0 (initial) is in the middle of the first box, already in the tutorial the subject box starts at zero.

Can anyone help me with this? I'm worried that along with the difference between a being at the beginning and another in the middle I get distortions in reading the information on my chart, impairing interpretation.

How should you get:

HowdoIget:

Thank you!

    
asked by anonymous 22.05.2016 / 18:51

1 answer

3

You just have to use boundary = 0 in geom_histogram to force the first bar to 0. Since you did not provide the data, I created an example, it got a bit different but seems to have solved:

data.combined <- data.frame(Age = rnorm(891, 35, 10),
                            Survived = sample(c(TRUE, FALSE), 891, TRUE),
                            Sex = sample(c("male", "female"), 891, TRUE),
                            Pclass = 1)

library(ggplot2)
ggplot(data.combined[1:891,], aes(x = Age, fill=Survived)) + 
  facet_wrap(~Sex + Pclass) + 
  geom_histogram(binwidth = 10, boundary = 0) + 
  xlab ("Age") + 
  ylab ("Total Count")

    
22.05.2016 / 19:53