Hey y’all, my goals for this week were to try replicate the remaining histograms and the error bar graph(s) in the group 5 paper.
Because I had already learned how to make a histogram last week, I thought it would be a much quicker process to make the remaining histograms. However, this was not the case as they were different aesthetically, and I had to learn a few new functions to make it work.
The histograms from the paper that were to be replicated look like this:
There were two new packages I had to install, “gridExtra” and “extrafont”.
“gridExtra” gives me the grid.arrange function that lets me put two graphs together.
“extrafont” gives me access to a variety of fonts to use, for these plots in particular, I needed Times New Roman.
After loading library(extrafont), you must use these in console to load the fonts. Then press ‘y’.
font_import()
## Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
## Continue? [y/n]
loadfonts(device="win")
I also came to the realisation that you can use theme presets, like theme_classic(), in conjunction with custom theme() code. This was very useful as it helped me exactly replicate the aesthetics of the plots above.
theme_classic() +
theme(text = element_text(family = "Times New Roman"), # This line sets the font of all text elements in the plot to Times New Roman.
plot.title = element_text(face = "bold", hjust = 0.5), # 'face = bold' lets me bold the title, and 'hjust = 0.5' lets me centre the title on the plot.
axis.line = element_line(colour = 'black', size = 0.75), # This line lets me alter the size and colour of the axis'.
axis.ticks = element_line(colour = "black", size = 0.75)) # This line lets me change
I added the above code chunk to both of my plots and then used grid.arrange to put the two together, which produced this output:
library(gridExtra)
library(extrafont)
p1 <- ggplot(Study2, (aes(BMPN_Diff))) +
ggtitle("Distribution of Relatedness Difference Scores") +
geom_histogram(binwidth = 0.5, boundary = 0, colour = 'black', fill = 'grey') +
labs(x = "Relatedness Difference Score (T2 - T1)", y = "Frequency") +
scale_x_continuous(limits = c(NA, 4), expand = c(0.015, 0.015)) + #might have to play around with expand?
scale_y_continuous(expand = c(0, 0), limits = c(0, 80)) +
theme_classic() +
theme(text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", hjust = 0.5),
axis.line = element_line(colour = 'black', size = 0.75),
axis.ticks = element_line(colour = "black", size = 0.75))
p2 <- ggplot(Study2, (aes(Lonely_Diff))) +
ggtitle("Distribution of Loneliness Scores") +
geom_histogram(binwidth = 0.1, boundary = 0, colour = 'black', fill = 'grey') +
labs(x = "Loneliness Difference Score (T2 - T1)", y = "Frequency") +
scale_x_continuous(limits = c(-2, 2), expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 50)) +
theme_classic() +
theme(text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", hjust = 0.5),
axis.line = element_line(colour = 'black', size = 0.75),
axis.ticks = element_line(colour = "black", size = 0.75))
grid.arrange(p1, p2, ncol = 2)
As you can see, the histograms I made are pretty much identical to the ones in the paper:
I couldn’t get the x-axis for the first histogram to cut off at 4 without setting the first bin at the y-axis. However, this is a very minor visual discrepency.
My goals for next week are to work with my team on the error bar line graphs and descriptive statistics: