Kylie-Ann’s tutorial

Install packages (if required) and then load them

Code
library(ggplot2)
library(gtsummary)

Basic Syntax

Code
# ggplot(dataset, aes(x = xvar))

# Setting axes
# ggplot(trial, aes(x = age)) 
# ggplot(trial, aes(x = factor(stage))) 

# Customise labels
# ggplot(trial, aes(x = factor(stage)))  +
  # ggtitle("Plot of Stage") +
  # xlab("Stage")

Bar Plot

Code
ggplot(trial, aes(x = factor(stage))) +
  geom_bar() +
  ggtitle("Bar Plot of Cancer Stage") +
  xlab("Cancer Stage") +
  ylab("Count")

Histograms

Code
#Add values and lables
ggplot(trial, aes(x = marker)) +
  geom_histogram() +
  ggtitle("Histogram of Marker Values") +
  xlab("Marker Value")

Code
#Change bin width  
ggplot(trial, aes(x = marker)) +
    geom_histogram(binwidth=0.5) +
    ggtitle("Histogram of Marker Values") +
    xlab("Marker Value") +
    ylab("Frequency")

Code
#Add colour
ggplot(trial, aes(x = marker)) +
      geom_histogram(binwidth=0.5,
                     colour="black",
                     fill="lightblue") +
      ggtitle("Histogram of Marker Values") +
      xlab("Marker Value") +
      ylab("Frequency")

Code
#Colour chart
#https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf



# Customise labels
ggplot(trial, aes(x = marker)) +
   geom_histogram(binwidth=0.5,
                     colour="black",
                     fill="lightblue") +
  ggtitle("Plot of Age vs Marker") +
  xlab("Age") +
  ylab("Marker")

Scatter plots

Code
ggplot(trial, aes(x = age, y = marker)) +
  geom_point() +
  ggtitle("Scatter Plot of Age vs Marker") +
  xlab("Age") +
  ylab("Marker")

Code
# Basic Scatter Plot + Line of Best fit
ggplot(trial, aes(x = age, y = marker)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  ggtitle("Scatter Plot of Age vs Marker") +
  xlab("Age") +
  ylab("Marker")

Code
ggplot(trial, aes(x = age, y = marker)) +
  geom_point(aes(colour=stage)) +
  scale_colour_manual(values=c("darkgreen", "lightblue", "red", "black")) +
  geom_smooth(method = "lm", se = FALSE) +
  ggtitle("Scatter Plot of Age vs Marker") +
  xlab("Age") +
  ylab("Marker")

Code
#dots slightly bigger, the dots have a red outline and green fill, make the line of best fit purple and panel by the variable stage and centre the title
ggplot(trial, aes(x = age, y = marker)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  ggtitle("Scatter Plot of Age vs Marker") +
  xlab("Age") +
  ylab("Marker")

Code
ggplot(trial, aes(x = age, y = marker)) +
  geom_point(size = 3, shape = 21, color = "red", fill = "green") + # larger dots with red outline and green fill
  geom_smooth(method = "lm", se = FALSE, color = "purple") + # line of best fit in purple
  facet_wrap(~ stage) + # panel by the variable stage
  ggtitle("Scatter Plot of Age vs Marker") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), # center and bold the title
    axis.title = element_text(size = 12), # increase axis title size
    axis.text = element_text(size = 10), # increase axis text size
    strip.text = element_text(size = 12) # increase facet strip text size
  ) +
  xlab("Age") +
  ylab("Marker")

Code
#Publication quality and greyscale
ggplot(trial, aes(x = age, y = marker)) +
  geom_point(size = 3, shape = 21, color = "black", fill = "grey70") + # larger dots with black outline and grey fill
  geom_smooth(method = "lm", se = FALSE, color = "black") + # line of best fit in black
  facet_wrap(~ stage) + # panel by the variable stage
  ggtitle("Scatter Plot of Age vs Marker") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), # center and bold the title
    axis.title = element_text(size = 12), # increase axis title size
    axis.text = element_text(size = 10), # increase axis text size
    strip.text = element_text(size = 12) # increase facet strip text size
  ) +
  xlab("Age") +
  ylab("Marker")

Density plots

Code
library(ggridges)
library(viridis)
ggplot(trial, aes(x = marker, y = stage, fill = ..x..)) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis(name = "Marker", option = "magma") + # using the magma color palette
  theme_minimal() +
  labs(
    x = "Marker",
    y = "Stage",
    title = "Density Plot of Marker by Stage"
  ) +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), # center and bold the title
    axis.title = element_text(size = 12), # increase axis title size
    axis.text = element_text(size = 10), # increase axis text size
    strip.text = element_text(size = 12) # increase facet strip text size
  )

Code
#https://r-graph-gallery.com
#https://www.cedricscherer.com/2019/08/05/a-ggplot2-tutorial-for-beautiful-plotting-in-r/