diff --git a/Tridimensional barplots in R/barplot_with_dots.R b/Tridimensional barplots in R/barplot_with_dots.R new file mode 100644 index 0000000000000000000000000000000000000000..99af6df23d921ee65009271ad132cd65ba53b249 --- /dev/null +++ b/Tridimensional barplots in R/barplot_with_dots.R @@ -0,0 +1,69 @@ +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") diff --git a/Tridimensional barplots in R/experimental_data.txt b/Tridimensional barplots in R/experimental_data.txt new file mode 100644 index 0000000000000000000000000000000000000000..0d22da144e4b8c62610f196988426307872a1e77 --- /dev/null +++ b/Tridimensional barplots in R/experimental_data.txt @@ -0,0 +1,13 @@ +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 diff --git a/Tridimensional barplots in R/initial.png b/Tridimensional barplots in R/initial.png new file mode 100644 index 0000000000000000000000000000000000000000..037ef833056eb59ff6dbc0926bb92206268b5678 Binary files /dev/null and b/Tridimensional barplots in R/initial.png differ diff --git a/Tridimensional barplots in R/melted.png b/Tridimensional barplots in R/melted.png new file mode 100644 index 0000000000000000000000000000000000000000..8c4e705f615dd11541d1b828632f175e5e43ead9 Binary files /dev/null and b/Tridimensional barplots in R/melted.png differ diff --git a/Tridimensional barplots in R/mybarplot.pdf b/Tridimensional barplots in R/mybarplot.pdf new file mode 100644 index 0000000000000000000000000000000000000000..98b4c5804bd0c85b5310efc02447d48ecde8352d Binary files /dev/null and b/Tridimensional barplots in R/mybarplot.pdf differ diff --git a/Tridimensional barplots in R/plot_inR.png b/Tridimensional barplots in R/plot_inR.png new file mode 100644 index 0000000000000000000000000000000000000000..f5d8f9827858e95791d437bca06d9abff76ce163 Binary files /dev/null and b/Tridimensional barplots in R/plot_inR.png differ diff --git a/Tridimensional barplots in R/savedplot.png b/Tridimensional barplots in R/savedplot.png new file mode 100644 index 0000000000000000000000000000000000000000..287dbeddd4e8904d7398f81c866eda3b2fd19199 Binary files /dev/null and b/Tridimensional barplots in R/savedplot.png differ