Learning Log Week 7

Goals this week

  1. Tidy up our codes
  2. Once again, trying to get the git push to work, may have to give up on it at this point😢😢😢😢
  3. Complete most of the group presentation slides

Successes

  1. Managed to tidy up the code, with a huge shout-out to Jenny who taught us about the function() during the Q&A. What was crazy is that the lines of code that we had totaled about 500 lines of code for 9 plots, excluding the histogram and now cuts it down to around to less than 100 lines which is INSANE!
  • Also side note, thanks to Torunn who showed us how to write the actual function() like this using back ticks, which apparently works on any message board including whatsapp!
  1. We ended up making significant process on our presentation, most people finished up their slides and scripts, we had a couple practice runs, critiqued it and are now in the process of finalising everything before our final recording on Tuesday!

Challenges

  1. Not too many challenges to speak of, it’s still the same issue that has been plaguing me for the past 2-3 weeks now which is github not pushing. I’ve tried to look into an array of solutions but it doesn’t seem to be working out and it is difficult to test out since I don’t want to end up deleting my other group member’s files.

Creating our own function

  • First things first, we assign figure.1.fun as our plot name using the output function(), and then specify all the variables that need to be changed from each plot. As you can see it includes: the y variable, the plot title, the y axis title and the upper and lower limits.
  • ggplot() is used to plot the graph including the x-axis and y axis and the backdrop.
  • geom_violin() is what creates the violin plot.
  • ggtitle(label = plot_title) is used to plot the plots. title.
  • theme_bw() is used to change the background colour from the normal grey to a black and white.
  • theme(plot.title = element_text(hjust = 0.5))is to align the title along the centre.
  • scale_x_discrete() is used to set the values for discrete x with (name = NULL) to allow for the default scale values.
  • scale_y_continuous()is the set of values and default scales for y aesthetics.
  • facet_wrap() is used to divide the plots by format and change the facet titles to be at the bottom of the plot rather than the top.
  • stats_summary() is used to indicate the condition mean and 95% confidence intervals with the argument fun.data = mean_cl_normal" indicating that we want to showcase the mean confidence intervals. The geom="crossbar" indicates for the CI bands and mean bars, fill = "white" changes the colour to white and alpha = .7 changes the transparency. easy_remove_legend() is from the ggeasy()packages and as it says in the function, just removes the legend from the plot. geom_beeswarm(cex = 0.2) is used to add the dot points along the plot `scale_fill_manual(values = c(“slategray2”, “lightpink1”))’ is to change the colour of the plots according to what’s listed!
# Making our own function!! 
figure.1.fun <- function(y_var, plot_title, y_title, lim_1, lim_2)  {
  
  ggplot(mydata,aes(x = Conflict, y = y_var, fill = Conflict)) +
  geom_violin() +
  ggtitle(label = plot_title) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_discrete(name = NULL) +
  scale_y_continuous(name = y_title, limits = c(lim_1, lim_2)) +
  facet_wrap(vars(Format), strip.position = "bottom") +
  stat_summary(fun.data = "mean_cl_normal", geom = "crossbar", fill = "white", alpha = .7) +
  easy_remove_legend() +
  geom_beeswarm(cex = 0.2) + 
  scale_fill_manual(values = c("slategray2", "lightpink1")) 
             
}

Now that the function is created, we define each unique component of the specific plot into the function, so for example: the contradiction plot has the y variable “Perceived Contradiction”, so typing that into the function formula will make it come out uniquely so!

#Plotting Contradiction, Advancement and Confusion plots using function 
contradiction.plot <- figure.1.fun(y_var = mydata$contradiction, plot_title = "Contradiction", y_title = "Perceived Contradiction", lim_1 = 1, lim_2= 30) 
advancement.plot <- figure.1.fun(y_var = mydata$advancement, plot_title = "Advancement", y_title = "Perceived Scientific Advancement", lim_1 = -1, lim_2 = 1)
confusion.plot <- figure.1.fun(y_var = mydata$confusion, plot_title = "Confusion", y_title = "Confusion", lim_1 = 1, lim_2 = 5)



# Combine plots using the package patchwork()
voltronplot1 <- contradiction.plot + advancement.plot + confusion.plot + plot_layout(ncol = 2)
print(voltronplot1)

Using patchwork() for the last bit just allows for all the plots to come together quite nicely

Experiment 2

In essence, it’s just doing the same for experiment 2, just a little bit longer since we’re working with a total of 6 plots now! And the following code is courtesy of my group who worked extremely hard to synthesize a master version for the code output!!

##For all plots
#Separate the data in Condition into 4 columns to separate levels of each IV  ("Block_1_Generic_Conflict"...)
mydata2 <- separate(mydata2, Condition, c("block", "number", "Format", "Conflict"))
#set these new IV columns as factors 
mydata2 <- mydata2 %>%
  mutate(Format=as.factor(Format)) %>%
  mutate(Conflict=as.factor(Conflict)) 
sapply(mydata2, class) #to check
#calculate the average for each scale -- dplyr
#nutritional confusion mean
mydata2 <- mydata2 %>%
  mutate(confusion = (NC_1 + NC_2 + NC_3 + NC_4 + NC_5 + NC_6)/6)
#nutritional backlash mean
mydata2 <- mydata2 %>%
  mutate(backlash = (NBS_1 + NBS_2 + NBS_3 + NBS_4 + NBS_5 + NBS_6)/6)
#Mistrust of expertise mean
mydata2 <- mydata2 %>%
  mutate(mistrust = (Mistrust_expertise_1 + Mistrust_expertise_2 +
    Mistrust_expertise_3)/3)
#Confidence in scientific community = single column ('GSS')
#Certainty of knowledge mean
mydata2 <- mydata2 %>%
  mutate(certainty = (Certainty_sci_know_1 + Certainty_sci_know_2 +
    Certainty_sci_know_3 + Certainty_sci_know_4 + Certainty_sci_know_5 + 
    Certainty_sci_know_6)/6)
#Development of knowledge mean
mydata2 <- mydata2 %>%
  mutate(development = (Development_sci_know_1 + Development_sci_know_2 +
    Development_sci_know_3 + Development_sci_know_4 + Development_sci_know_5 +
    Development_sci_know_6)/6)
#Export the FINAL data to a .csv
write_csv(mydata2, "MyDataFinalSubset2.csv")
##Violin ggplots
#Make our own function
figure.2.fun <- function(y_var, plot_title, y_title, lim_2 = 5) {
  ggplot(mydata2, aes(x= Conflict, y = y_var, fill = Conflict)) +
  geom_violin() +
  facet_wrap(vars(Format), strip.position = "bottom") +
  stat_summary(fun.data = "mean_cl_normal",geom = "crossbar", fill = "white",
    alpha = .7) +
  geom_beeswarm(cex = 0.2) +
  ggtitle(label = plot_title) +
  easy_center_title() +
  easy_remove_legend() +
  scale_x_discrete(name = NULL) +
  scale_y_continuous(name = y_title, limits= c(1,lim_2)) +
  scale_fill_manual(values = c("slategray2", "lightpink1")) } 
#Use function to make plots
nutritionalconfusion.plot <- figure.2.fun(y_var = mydata2$confusion, plot_title = "Nutritional Confusion", y_title = "Nutritional Confusion")
nutritionalbacklash.plot <- figure.2.fun(y_var = mydata2$backlash, plot_title = "Nutritional Backlash", y_title = "Nutritional Backlash")
mistrust.plot <- figure.2.fun(y_var = mydata2$mistrust, plot_title = "Mistrust of Expertise", y_title = "Mistrust of Expertise")
confidence.plot <- figure.2.fun(y_var = mydata2$GSS, plot_title = "Confidence in Scientific Community", y_title = "Confidence in Scientific Community", lim_2 = 3)
certainty.plot <- figure.2.fun(y_var = mydata2$certainty, plot_title = "Certainty of Knowledge", y_title = "Certainty of Knowledge")
development.plot <- figure.2.fun(y_var = mydata2$development, plot_title = "Development of Knowledge", y_title = "Development of Knowledge")
print(nutritionalconfusion.plot)

print(nutritionalbacklash.plot)

print(mistrust.plot)

print(confidence.plot)

print(certainty.plot)

print(development.plot)

# Combine plots using the package patchwork()
voltronplot2 <- nutritionalconfusion.plot + nutritionalbacklash.plot + mistrust.plot + confidence.plot + certainty.plot + development.plot + plot_layout(ncol = 2)
print(voltronplot2)

Goals for next week

  1. Finalise all our slides and record our final presentation!
  2. Start the exploratory analysis
  3. Edit S&R of the article, kept forgetting about this~
  4. Start on the verification report

I would like to just once again, thank my team for the tremendous effort they went into coding this! My team members: Torunn, Jia, Lauren and Sam did an amazing job and were constantly on the ball. Honestly I think I fell a bit short and don’t think I would have made anywhere nearly as much progress without their help. So thank you guys ❤️