Data presentation in ggplot2

Asked

Viewed 111 times

5

Hi, I’m having trouble at ggplot2 on 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 of a tutorial I’m getting a histogram in which the value of 0 (initial) is in the middle of the first box, already in the tutorial the subject box starts at zero.

Can someone help me with this? I am worried because along the difference between one being in the beginning and the other in the middle I get distortions in reading the information on my chart, harming the interpretation.

How should I get:

inserir a descrição da imagem aqui

How do I get:

inserir a descrição da imagem aqui

Thank you very much!

1 answer

4


Apparently you just need to wear boundary = 0 in the geom_histogram to force the first bar to 0. Since you did not provide the data, I created an example, it was a little 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")

inserir a descrição da imagem aqui

  • Hello! Actually, it has greatly improved my visualization already. However, would you like to tell me why I still have some distortions to my chart? For example, if you click here and observe his at 23:04: https://youtu.be/u6sahb7Hmog?t=23m4s compared to what I’m getting now, do you know why the distortions between me and him? Here’s my current: Atual

  • @Guilhermeheiden It is difficult to say just by looking at the two images, but I imagine they can be two causes: 1. You are using Windows, and it OSX, the graphics devices are different and may be changing something. 2 (most likely). The video is from 2014, and who knows when it was recorded, then the versions of R and the ggplot2 are different and changes may have been made. If you are just taking the course, do not worry about these details, when it is in fact create a chart there yes it is worth going after the details.

  • Thank you so much @Molx! You’ve been so helpful and understanding, I’ll follow then, hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.