####---- PCA Scatter Plot Function ----####

make_PCA_scatterplot <- function(plot_sample_groupings,comp_x,comp_y,x_axis_label,y_axis_label,plot_sample_group_colours,plot_sample_group_labels,plot_sample_labels,dot_size,dot_transparency,legend_position,sample_label_size,show_proportion_of_variance, show_sample_labels)
{

  # Do PCA
  prcomp_data = prcomp(as.matrix(sapply(ne_matrix_scaled_transposed, as.numeric)))
  prcomp_coordinates = data.frame(prcomp_data$x)

  # Get variance
  if (show_proportion_of_variance == TRUE)
  {
    vars = apply(prcomp_data$x, 2, var)
    prop_x = round(vars[comp_x] / sum(vars),4) * 100
    prop_y = round(vars[comp_y] / sum(vars),4) * 100

    x_axis_label = paste(x_axis_label, " (",prop_x,"%)",sep="")
    y_axis_label = paste(y_axis_label, " (",prop_y,"%)",sep="")
  }

  # Prepare Table
  prcomp_coordinates = prcomp_coordinates[,c(component_x, component_y)]
  names(prcomp_coordinates) = c("comp_x","comp_y")

  # labels
  if (show_sample_labels == TRUE)
  {
    plot_sample_labels = plot_sample_labels
  }
  else
  {
    plot_sample_labels = NA
  }

  #make plot
  ggp = ggplot(data=prcomp_coordinates, aes(x=comp_x, y=comp_y, colour=plot_sample_groupings)) +
    geom_point(size=dot_size,alpha=dot_transparency) +
    geom_label_repel(aes(label=plot_sample_labels), size=sample_label_size, force = 1,box.padding = 1.5, label.padding = unit(0.5, "lines"), show.legend = FALSE, colour = "black") +
    scale_color_manual(values=plot_sample_group_colours,labels=plot_sample_group_labels, name="") +
    xlim(c(min(prcomp_coordinates$comp_x)*1.25,max(prcomp_coordinates$comp_x)*1.25)) +
    ylim(c(min(prcomp_coordinates$comp_y)*1.25,max(prcomp_coordinates$comp_y)*1.25)) +
    labs(x=x_axis_label, y=y_axis_label) +
    theme_SL2() +
    theme(legend.position=legend_position, legend.spacing.x = unit(0.25, 'cm'))

  return(ggp) 
}

