library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ✔ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
setwd("~/shiangchiet/VinMec/")

data <- read.csv("input.csv", row.names = 1, header = TRUE)

#Figure A
dataA <- subset(data, sample == "A")
data.melt <- reshape2::melt(dataA, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_A <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject A") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_A)

ggsave(filename= 'output/A.png', plot = fig_A, height=10, width= 16)

R Markdown

#Figure B

dataB <- subset(data, sample == "B")
data.melt <- reshape2::melt(dataB, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_B <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject B") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_B)

ggsave(filename= 'output/B.png', plot = fig_B, height=10, width= 16)

Including Plots

You can also embed plots, for example:

#Figure C

dataC <- subset(data, sample == "C")
data.melt <- reshape2::melt(dataC, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_C <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject C") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_C)

ggsave(filename= 'output/C.png', plot = fig_C, height=10, width= 16)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#Figure D
dataD <- subset(data, sample == "D")
data.melt <- reshape2::melt(dataD, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_D <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject D") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_D)

ggsave(filename= 'output/D.png', plot = fig_D, height=10, width= 16)
#Figure E
dataE <- subset(data, sample == "E")
data.melt <- reshape2::melt(dataE, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_E <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject E") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_E)

ggsave(filename= 'output/E.png', plot = fig_E, height=10, width= 16)
#Figure F
dataF <- subset(data, sample == "F")
data.melt <- reshape2::melt(dataF, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_F <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject F") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_F)

ggsave(filename= 'output/F.png', plot = fig_F, height=10, width= 16)
#Figure G
dataG <- subset(data, sample == "G")
data.melt <- reshape2::melt(dataG, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_G <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject G") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_G)

ggsave(filename= 'output/G.png', plot = fig_G, height=10, width= 16)
#Figure H
dataH <- subset(data, sample == "H")
data.melt <- reshape2::melt(dataH, na.rm = TRUE) 
## Using sample, Timepoint as id variables
colnames(data.melt)[4] = "score" 
data.melt$Timepoint <- factor(data.melt$Timepoint, levels = c("Pre-probiotic","Post-probiotic"))

fig_H <- ggplot(data.melt, aes(Timepoint, score)) + 
   geom_bar(stat="identity", aes(fill = Timepoint)) +
   ggtitle("Subject H") +
   theme(axis.text.x = element_text(vjust=0.6, angle=45, size = 6)) + 
   theme_bw() +
   facet_wrap(~variable, ncol=5)

plot(fig_H)

ggsave(filename= 'output/H.png', plot = fig_H, height=10, width= 16)