My goals for this week include spicing up my markdown document with a table of contents and some other aesthetic changes. I also hope to develop some basic knowledge of ggplot and perhaps even create an APA style (-ish) graph. Oh, and I must learn how to use ‘here’ package using Jenny’s helpful guide which can be found HERE! (I have also learnt how to attach a link to text!)
First I must install, load and test the here package.
install.packages("here")
library(tidyverse)
library(here)
here::here()
## [1] "/Users/lukekeevers/Desktop/Uni/Third Year/T1/3361 (Internship)"
here("data", "chickflick.csv")
## [1] "/Users/lukekeevers/Desktop/Uni/Third Year/T1/3361 (Internship)/data/chickflick.csv"
Aha! I have realised that including the “install.packages” function makes it so the package is re-installed every time I knit the document. So I will include “eval=FALSE” into the code chunk to stop it from running this code after I have installed the package.
Honestly, I’m not really sure what the “here” function just did but it seems to load the data correctly…
chickflick <- read.csv(here("data", "chickflick.csv"), header = TRUE)
I will be using what I have learnt from Danielle’s guides to create a graph which visualises the different arousal levels of men and women to two movies. I got this example data from Dr Erin Buchanan’s site here.
First I will factor the variables appropriately.
chickflick$gender = factor(chickflick$gender,
levels = c(1,2),
labels = c("Men", "Women"))
chickflick$film = factor(chickflick$film,
levels = c(1,2),
labels = c("Bridget Jones", "Memento"))
Time to create the bar chart!
chickbar <- ggplot(data = chickflick,
mapping = aes(x = film, y = arousal, fill = gender))
print(chickbar)
Okay…so I knew this wouldn’t produce the final product but this is less than I was expecting. I forgot about what Danielle said about plots in ggplot using “layers”. So I went online and figured out how to add my mean bars and some confidence interval error bars.
So the “stat_summary” function allows me to add my layers. The “geom” argument specifies what object I want to represent my selected function, “fill” changes the colour of my object, and “colour” changes the colour of the outline.
For the error bars, the “width” argument allows me to change the width of the bars, whilst the “position =”dodge"" part makes sure parts of the graph don’t get in the way of each other. Finally, I add some x and y labels and… now I get a much nicer looking graph.
chickbar <- chickbar +
stat_summary(fun.y = mean,
geom = "bar",
position = "dodge",
colour = "black") +
stat_summary(fun.data = mean_cl_normal,
geom = "errorbar",
width = .2, position = position_dodge(width = 0.90)) +
xlab("Film Watched") +
ylab("Arousal Level")
print(chickbar)
It is time to make this nice looking graph a bit more science-journal-appropriate. First I can fix the legend to have more appropriate labels and I can change the colour of the bars. Even though I don’t see many colourful graphs in the papers I read, I want mine to actually be interesting to look at. I found a list of all the ggplot colours here.
chickbar <- chickbar +
scale_fill_manual(name = "Gender of Participants",
labels = c("Male", "Female"),
values = c("deepskyblue1", "magenta1"))
print(chickbar)
I also wanted to know how to get rid of the ugly grey background. Luckily, the internet has a solution to that too! I think I will be using these particular lines of code often in the future, so I will assign them their own term - “background”.
background <- theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))
chickbar <- chickbar + background
print(chickbar)
And finally - add a title!
chickbar <- chickbar +
ggtitle(label = ("Arousal of Men and Women to Two Hit Movies"),
subtitle = ("A Nobel Prize-deserving finding"))
print(chickbar)
This week I embarked on my journey into ggplot. I managed to create a fairly attractive looking graph and also added to the knowledge I gained from Danielle’s videos. I also became more familiar with markdown and this cool cheatsheet.
One challenge I faced this week was how to minimise my code repetition everytime I wanted to add to my graph, whilst still displaying the graph at each step of the way. The best solution I found was to just keep updating my graph “chickbar” as I went along. That way, I update the graph with each addition and show the difference between each variaton whilst minimising the repetition in my code. I still don’t quite understand the “here” package, so maybe that’s something to improve on.
Next I hope to dive into Danielle’s data wrangling guides and improve my ggplot skills even further.