My Coding Goals This Week

  • My main goal this week was to make a concise version of our group’s Master Code for presenting next week (week 8).

How I Made Progress

Learning about Functions in Week 7 Q&A

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!!

Cleaning up my code

  • Using across() to make more concise my code, from this:
# 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! :)

Successes

  • Learning how to change settings in R Markdown to -> Chunk output in console!
  • Learning about writing functions, and across() - and seeing how many lines of code became redundant - very cool!

Challenges

  • I found that going back to make my code consistent was a little challenging (albeit doable)! As I started Experiment 1 with some more complex functions I found via Google that I did not necessarily understand what was going on - and then as we progressed through the weeks I realised there were simpler ways that I could do it. So there were some inconsistencies in doing the same thing with different functions - I went back to edit so that it all is consistent now.

Next Steps in My Coding Journey

  • As we start thinking about exploratory analysis next week, I think my next steps will be to look back at the initial data set and start thinking about what kind of exploratory analysis I could look at, and what functions I can use to explore the data with!