Jenny S taught us how to write functions in Q&A session. Since we have 9 plots that use similar codes, writing a function meant we could be less repetitive and have a shorter, more concise code!
#Plot Contradiction
contradiction <- ggplot(
data = exponefinaldata,
aes(
x = conflict,
y = contradiction,
fill = conflict
)
) +
geom_violin() +
ggtitle(
label = "Contradiction"
) +
theme(
plot.title = element_text(hjust = 0.5) #center the plot title
) +
scale_x_discrete(
name = NULL
) +
scale_y_continuous(
name = "Perceived Contradiction",
limits = c(0,30)
) +
facet_wrap(
vars(format),
strip.position = "bottom"
) +
stat_summary( #adding crossbars to indicate mean and 95% confidence intervals
fun.data = "mean_cl_normal", #from the Hmisc package, mean_cl_normal function used to calculate and display 95% CIs
geom = "crossbar", #specifying we want crossbars
fill = "white", #changing crossbar fill colour
alpha = .7 #changes transparency of fill to 70%
) +
easy_remove_legend() +
geom_beeswarm(
cex = 0.2 #add a bee swarm plot (one-dimensional scatter plot) to show all data points, cex specified width
) +
scale_fill_manual(
values = c("slategray2", "lightpink1") #change violin plot fill colours
)
plot(contradiction)
#Making our function
figure.1.fun <- function(y_var, plot_title, y_title, lim_1, lim_2) {
ggplot(exponefinaldata,aes(x = conflict, y = y_var, fill = conflict)) +
geom_violin() +
ggtitle(label = plot_title) +
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"))
}
#Plotting Contradiction, Advancement and Confusion plots using function
contradiction.plot <- figure.1.fun(y_var = exponefinaldata$contradiction, plot_title = "Contradiction", y_title = "Perceived Contradiction", lim_1 = 1, lim_2= 30)
advancement.plot <- figure.1.fun(y_var = exponefinaldata$advancement, plot_title = "Advancement", y_title = "Perceived Scientific Advancement", lim_1 = -1, lim_2 = 1)
confusion.plot <- figure.1.fun(y_var = exponefinaldata$confusion, plot_title = "Confusion", y_title = "Confusion", lim_1 = 1, lim_2 = 5)
#Combining plots
combineplots1 <- contradiction.plot + advancement.plot + confusion.plot + plot_layout(ncol = 2)
print(combineplots1)
As seen above, the function is as long as the initial code for one plot - but we had initially included 3 x the first set of code!!
# Change from Chr to Numeric for all contradiction variables
exponefinaldata$contradiction_1 <- as.numeric(exponefinaldata$contradiction_1)
exponefinaldata$contradiction_2 <- as.numeric(exponefinaldata$contradiction_2)
exponefinaldata$contradiction_3 <- as.numeric(exponefinaldata$contradiction_3)
exponefinaldata$contradiction_4 <- as.numeric(exponefinaldata$contradiction_4)
exponefinaldata$contradiction_5 <- as.numeric(exponefinaldata$contradiction_5)
exponefinaldata$contradiction_6 <- as.numeric(exponefinaldata$contradiction_6)
exponefinaldata$confusion <- as.numeric(exponefinaldata$confusion)
exponefinaldata$advancement <- as.numeric(exponefinaldata$advancement)
To this:
# Change variables from Chr to Numeric/Factor accordingly
exponefinaldata <- exponefinaldata %>%
mutate(
across(c(Age,contradiction_1:advancement), as.numeric)
)
I went ahead and did this for the other experiment too - and condensed almost 30 lines of code into 5. Very satisfying! :)