My histogram starts at the wrong number

I'm learning R through ' Hands-On Programming with R'. I've run into a problem with histograms- my histogram does not match the one in the book.

This is the code I used:

library(ggplot2)
x2 <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4)
qplot(x2, binwidth = 1)

and the code in the textbook: (ggplot2 was loaded earlier)

x2 <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4)
qplot(x2, binwidth = 1)

This is the graph I got:

I can't upload a second image for the graph from the textbook.

My graph starts at the wrong number, in the textbook, the first bar sits between 1 and 2 on the x-axis
It is different in other ways too- it does not have a y-axis label, and the columns seem broader than the example in the textbook. What have I misunderstood?

I believe the first edition of that book was published 7 years ago.
To get the same results I would expect that you would need to be aligned to the same ggplot2 version as that of the book. What edition of the book do you have? does it say what version of ggplot2 it was using ?

Sidenote that 2months ago the ggplot2 devlopment code added the following comments:

#' `qplot()` is now deprecated in order to encourage the users to
#' learn [ggplot()] as it makes it easier to create complex graphics.

So I would suggest not to worry about it, and focus more on ggplot2 lessons, whether on the book as mentioned , or the r4ds book

1 Like

I'm not sure how to find the edition, I'm using what I understand to be the free online version linked to RStudio.

It didn't mention the version of ggplot2 that it uses.

Thank you for the advice!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.