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!function() like this using back ticks, which apparently works on any message board including whatsapp!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
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)
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 ❤️