Skip to content
Snippets Groups Projects
Commit 32958635 authored by Cosmin  SAVEANU's avatar Cosmin SAVEANU
Browse files

Update Tridimensional barplots in R/barplot_with_dots.R, Tridimensional...

Update Tridimensional barplots in R/barplot_with_dots.R, Tridimensional barplots in R/experimental_data.txt, Tridimensional barplots in R/initial.png, Tridimensional barplots in R/melted.png, Tridimensional barplots in R/plot_inR.png, Tridimensional barplots in R/savedplot.png, Tridimensional barplots in R/mybarplot.pdf files
parent ac58cab8
No related branches found
No related tags found
No related merge requests found
mydata <- read.delim("experimental_data.txt", stringsAsFactors = F)
#strain vector exp1 exp2 exp3
# 1 wt vector1 0.981 1.011 1.008
# 2 wt vector2 0.712 0.724 0.818
# 3 wt vector3 0.135 0.426 0.402
# 4 wt vector4 0.161 0.426 0.369
# 5 mutA vector1 1.028 1.089 1.171
# 6 mutA vector2 0.972 1.096 1.193
# .....
library(reshape2)
# convert the table to a "longer" data format, but preserve
# the dimensions that will be useful for plotting, here by index
melted <- melt(mydata, id.vars=c(2,1))
names(melted) <- c("vector", "strain", "experiment", "value")
# vector strain experiment value
# 1 vector1 wt exp1 0.981
# 2 vector2 wt exp1 0.712
# 3 vector3 wt exp1 0.135
# 4 vector4 wt exp1 0.161
# 5 vector1 mutA exp1 1.028
# 6 vector2 mutA exp1 0.972
# 7 vector3 mutA exp1 0.141
# 8 vector4 mutA exp1 0.292
# .....
melted$strain <- factor(x=melted$strain, levels=c("wt", "mutA", "mutB"))
# manually setting the "strain" as a factor with the levels in a given order
# ensures that the plotting of thses categories will be done in this order
library(dplyr)
# compute mean and standard deviation of values, grouped by vector AND strain
averages <- melted %>% group_by(vector, strain) %>% summarise(avg=mean(value))
sds <- melted %>% group_by(vector, strain) %>% summarise(sd=sd(value))
# create columns that will serve for the range of the error bars
averages$ymin <- averages$avg-sds$sd
averages$ymax <- averages$avg+sds$sd
# changing the position of the bars in the barplot by the same amount
# is crucial to align the dots, error bars and bars.
# the following value is used three times in the plot, adjust to your liking
dodge_value <- 0.8
ggplot(data=averages,
aes(x=vector, y=avg, color=strain, fill=strain))+
geom_col(width=0.7, position = position_dodge(dodge_value))+
scale_fill_manual(values = rep(c("gray30", "gray70", "white"), 4))+
# this needs to be adjusted and modified according to your needs
# for 4 vectors and 3 strains, three shades of gray can be enough
scale_color_manual(values = rep("black", 12))+
# this parameter ensures that error bars and bar outlines are all black
geom_point(data=melted,
aes(x=vector, y=value, color=strain),
shape=21, fill="white",
position=position_jitterdodge(dodge.width=dodge_value, jitter.width = 0.3))+
# add the data points. Even if the color is black for all the points, that aesthetic
# parameter is required to tell ggplot2 that we want grouping by strain
geom_errorbar(data=averages,
aes(ymin=ymin, ymax=ymax),
position = position_dodge(dodge_value),
width=0.2)+
# error bars, as SD
ylim(0, 1.4)+
# axis limits, to be adjusted to the range of the data, or remove to do it automatically
# the following parameters affect the plot and are especially useful for the pdf output
theme_classic(base_size=10)+
theme(text=element_text(size=6, family="ArialMT"), legend.key.size = unit(0.3, 'cm'))
ggsave("mybarplot.pdf", width=8, height=5, units="cm")
strain vector exp1 exp2 exp3
wt vector1 0.981 1.011 1.008
wt vector2 0.712 0.724 0.818
wt vector3 0.135 0.426 0.402
wt vector4 0.161 0.426 0.369
mutA vector1 1.028 1.089 1.171
mutA vector2 0.972 1.096 1.193
mutA vector3 0.141 0.311 0.385
mutA vector4 0.292 0.349 0.331
mutB vector1 1.054 1.078 1.07
mutB vector2 0.573 0.834 0.683
mutB vector3 1.063 0.986 0.947
mutB vector4 0.679 0.804 0.616
Tridimensional barplots in R/initial.png

30.9 KiB

Tridimensional barplots in R/melted.png

16.1 KiB

File added
Tridimensional barplots in R/plot_inR.png

17.9 KiB

Tridimensional barplots in R/savedplot.png

37.5 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment