I decided to go through my statistics courses, which are taught in SPSS, but do in R, as I would like to learn to do stats there. I am currently doing histograms for two numerical continuous varables data$alcohol
(alcohol misuse scale score) and data$age
but got stuck on the first one.
The main issues are:
- My histogram looks different from the picture in the answer sheet
- I cannot add a normal curve unless I change the
aes
to density, which I do not want to do, as the exercise asks for frequency
Here is what I wrote:
data <- read_excel("~/Dropbox/My Mac (jmbp.local)/Desktop/Kings College London/2021:2022/Statistics/Week 1 stats/cleandata.xlsx")
mean_alc <- mean(data$alcohol)
sd_alc <- sd(data$alcohol)
p <- ggplot(data= data) +
geom_histogram(mapping = aes(x = alcohol, y=..count..),
breaks=seq(0, 20, by=1),
col="black",
fill="white",
alpha = 1) +
labs(title="Alcohol Misuse Score", x="Alcohol Misuse Score", y="Frequency") +
xlim(c(0,20)) +
ylim(c(0,20)) +
stat_function(fun = dnorm, colour = "red", args=list(mean = mean(data$alcohol), sd = sd(data$alcohol))) +
plot(density(data$alcohol, bw = 0.05))
p
my histogram looks like this:
the picture included in the solutions (done in SPSS) looks like this:
My first question is why the bars in my histogram look different than the ones in the answer sheet? is there some fundamanetal difference in how SPSS does histograms and how R does it? Secondly, is there a way to add a normal curve to the frequency histogram in ggplot 2? For reference this is how this can be done in SPSS:
Frequency histogram with normal curve in SPSS
the data$alcohol
has the following values:
alcohol = c(15.78121, 17, 17.61943, 17.61943, 14.67395, 17.61943, 17, 17, 13.72413, 13.72664, 17, 15.86039, 17, 15.78121, 11.48049, 14.61672, 12.73437, 8, 17, 15.86039, 14.59133, 15.78121, 14.61672, 17, 17, 18, 15.78121, 10, 14.67395, 9, 7.033369, 17, 17, 15.86039, 15.78121, 18, 13.07577, 18, 8, 17.61943, 15.86039, 11.53364, 11.4323, 18, 6.390277, 17, 14.59133, 18, 14.9238, 15.78121, 14.61672, 17, 17.61943, 14.67395, 8, 18, 8, 17.61943, 14.4069, 6.477451, 7.02489, 18, 18, 13.09201, 15.78121, 14.59133, 18, 5.451102, 9, 4.801972, 15.86039, 15.86039, 17, 17, 17)