Useful websites:

  1. All about graphs in R: https://r-graph-gallery.com/
  2. Box plot: https://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization
  3. Bar plot: https://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization

Let’s begin!

First you need to load libraries. If you don’t have them, first run install.packages(c(“tidyverse”, “ggplot2”, “ggthemes”,“rio”))

# load libraries
library("ggplot2")
library("ggthemes")
library("rio")
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr     1.2.0     âś” readr     2.2.0
## âś” forcats   1.0.1     âś” stringr   1.6.0
## âś” lubridate 1.9.5     âś” tibble    3.3.1
## âś” purrr     1.2.1     âś” tidyr     1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Upload .csv files: In RStudio, locate the file and set the folder location as your working directory.

Then, you need to import your files

# Import files
df_1 = import('Fly_Nuclei.csv')
df_2 = import('Patient_Tumour.csv')
df_3 = import('PrestoBlue_Fluorescence.csv')

EXAMPLE 1: Fly Nuclei Area

Check your data

head(df_1)
##   condition sample nuclei area
## 1 48h_Notch      1      1  345
## 2 48h_Notch      1      2  346
## 3 48h_Notch      1      3  347
## 4 48h_Notch      1      4  348
## 5 48h_Notch      1      5  349
## 6 48h_Notch      1      6  350

Let’s start by making a basic box plot

# Basic box plot
p = ggplot(df_1, aes(x=condition, y=area)) + 
  geom_boxplot()
p

Add the individual values

p = ggplot(df_1, aes(x=condition, y=area)) + 
  geom_boxplot() +
  geom_jitter(shape=16, position=position_jitter(0.2))
p

No colours! Add colours

# Change box plot colors
p = ggplot(df_1, aes(x=condition, y=area, fill = condition)) + 
  geom_boxplot() +
  geom_jitter(shape=16, position=position_jitter(0.2))
p

Personalise the colours

# Use brewer color palettes
p = ggplot(df_1, aes(x=condition, y=area, fill = condition)) + 
  geom_boxplot() +
  geom_jitter(shape=16, position=position_jitter(0.2)) +
  scale_fill_brewer(palette="Dark2")
p

If you want other colours check here for more examples: https://datavizs24.classes.andrewheiss.com/resource/colors.html

Add sample information

p = ggplot(df_1, aes(x=condition, y=area, fill = condition, colour = sample)) + 
  geom_boxplot() +
  geom_jitter(shape=16, position=position_jitter(0.2)) +
  scale_fill_brewer(palette="Dark2") +
  scale_color_gradient(low = "blue", high = "red")
p
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

And finally, change labels

p = ggplot(df_1, aes(x=condition, y=area, fill = condition, colour = sample)) + 
  geom_boxplot() +
  geom_jitter(shape=16, position=position_jitter(0.2)) +
  scale_fill_brewer(palette="Dark2") +
  scale_color_gradient(low = "blue", high = "red") +
  xlab (" CONDITION BLA BLA") + ylab("NUCLEI AREA BLA BLA") +
  ggtitle("TITLE") +
  theme(axis.text = element_text(size = 12),
        axis.title.x = element_text(size = 16),
        axis.title.y = element_text(size = 16),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        text = element_text(size = 20),
        plot.title = element_text(size = 18, face="bold")) +
  theme(aspect.ratio=1) +
  theme(legend.position="top") # label position
p
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

And save

ggsave("Fly_Nuclei-Area.jpeg")
## Saving 7 x 5 in image
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

EXAMPLE 2: Patient Tumour

Check the data

# view dataframe
head(df_2)
##   Patient_ID Gender   Marker Area
## 1        341 Female marker 1  456
## 2        341 Female marker 2  234
## 3        345 Female marker 1  567
## 4        345 Female marker 2  765
## 5        348   Male marker 1  654
## 6        348   Male marker 2  864

Design a bar-plot

p = ggplot(df_2, aes(x=as.factor(Patient_ID), y=Area, fill = Marker)) +
  geom_bar(stat = "identity") +
  scale_fill_brewer(palette="Dark2") 
p

Improve visualisation

p = ggplot(df_2, aes(x=as.factor(Patient_ID), y=Area, fill = Marker)) +
  geom_bar(stat = "identity", position=position_dodge()) +
  scale_fill_brewer(palette="Dark2") 
p

If you want to separate the data according to another factor

# split according to gender or marker
p = ggplot(df_2, aes(x=as.factor(Patient_ID), y=Area, fill = Gender)) +
  geom_bar(stat = "identity", position=position_dodge()) +
  facet_wrap(~Marker) +
  scale_fill_brewer(palette="Dark2")
p

And finish with labels and new theme

p = ggplot(df_2, aes(x=as.factor(Patient_ID), y=Area, fill = Gender)) +
  geom_bar(stat = "identity", position=position_dodge()) +
  facet_wrap(~Marker) +
  scale_fill_brewer(palette="Dark2") +
  xlab (" CONDITION BLA BLA") + ylab("NUCLEI AREA BLA BLA") +
  ggtitle("TITLE") +
  theme(axis.text = element_text(size = 12),
        axis.title.x = element_text(size = 16),
        axis.title.y = element_text(size = 16),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        text = element_text(size = 20),
        plot.title = element_text(size = 18, face="bold")) +
  theme_linedraw() # change themes
p 

To find other themes: https://ggplot2-book.org/themes.html

Save

ggsave("Patient_tumour.jpeg")
## Saving 7 x 5 in image

EXAMPLE 3: Presto Blue Cell Viability (Fluorescence)

Check the data

# view dataframe
head(df_3)
##     condition duplicate_1 duplicate_2 average sd.average
## 1        DMSO        3455        4567    4011     786.30
## 2  2mM drug A        2980        2780    2880     141.42
## 3 20mM drug A        1567        1345    1456     156.98

Ups! An extra row. Remove extra row

#remove row 4
df_3 = df_3[-4,]

Make a bar plot

p = ggplot(df_3, aes(x=condition, y=average, fill = condition)) +
  geom_bar(stat = "identity") +
  scale_fill_brewer(palette="Dark2") 
p

Reorder

p = ggplot(df_3, aes(x=fct_relevel(condition, c("DMSO", "2mM drug A","20mM drug A")), y=average, fill = condition)) +
  geom_bar(stat = "identity") +
  scale_fill_brewer(palette="Dark2")
p

More info on ordering: https://blog.albertkuo.me/post/2022-01-04-reordering-geom-col-and-geom-bar-by-count-or-value/#

Add error bars

# add error bars
p = ggplot(df_3, aes(x=fct_relevel(condition, c("DMSO", "2mM drug A","20mM drug A")), y=average, fill = condition)) +
  geom_bar(stat = "identity") +
  geom_errorbar( aes(x=fct_relevel(condition, c("DMSO", "2mM drug A","20mM drug A")),
                     ymin=average-sd.average, ymax=average+sd.average), 
                 width=0.4, colour="orange", alpha=0.5, linewidth =1.3) +
  scale_fill_brewer(palette="Dark2")
p

And finish with labels and new theme

# all together
p = ggplot(df_3, aes(x=fct_relevel(condition, c("DMSO", "2mM drug A","20mM drug A")), y=average, fill = condition)) +
  geom_bar(stat = "identity") +
  geom_errorbar( aes(x=fct_relevel(condition, c("DMSO", "2mM drug A","20mM drug A")),
                     ymin=average-sd.average, ymax=average+sd.average), 
                 width=0.4, colour="orange", alpha=0.5, linewidth =1.3) +
  scale_fill_brewer(palette="Dark2") +
  xlab (" CONDITION BLA BLA") + ylab("NUCLEI AREA BLA BLA") +
  ggtitle("TITLE") +
  theme(axis.text = element_text(size = 12),
        axis.title.x = element_text(size = 16),
        axis.title.y = element_text(size = 16),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        text = element_text(size = 20),
        plot.title = element_text(size = 18, face="bold")) +
  theme_linedraw() # change themes
p

Save

# save
ggsave("PB_fluorescence.jpeg")
## Saving 7 x 5 in image

And voilĂ ! I think these are the most useful commands for you. Feel free to explore and contact me if you would like to plot something different :)