Commit c7bb6204 authored by Florian Huber's avatar Florian Huber

Added .R script from DataVis and exploration.

parent 8d5eb06d
## ---- message = FALSE----------------------------------------------------
library(tidyverse)
library(ggbeeswarm)
## ---- eval = FALSE, include = FALSE--------------------------------------
## data("x")
## x
## ?x
## dim(x)
## exprs(x)
## class(exprs(x))
## exprs(x)[1:5, 1:5]
##
## selectedProbes <- c( Fgf4 = "1420085_at", Gata4 = "1418863_at",
## Gata6 = "1425463_at", Sox2 = "1416967_at")
##
## genes <- as_tibble(exprs(x), rownames = "probe")
##
## genes <- filter(genes, probe %in% selectedProbes) %>%
## gather(key = sample, value = value, -probe)
## genes
##
## genes$gene <- names(selectedProbes)[ match(genes$probe, selectedProbes) ]
##
## ##
##
## head(pData(x), n = 2)
## class(pData(x))
##
## (metadata <- as_tibble(pData(x), rownames = "rowname"))
##
## groups <- group_by(metadata, sampleGroup) %>%
## dplyr::summarise(n = n(), colour = unique(sampleColour))
## groups
## ------------------------------------------------------------------------
# brackets around an expression will evaluate it so you don't have to type
# cells again after assignment to see its contents
(cells <- read_delim("../data/ImagedCells.csv", delim = ";"))
## ---- eval = FALSE-------------------------------------------------------
## # not run: run in the console to see the output
## str(cells)
## glimpse(cells)
## head(cells)
## summary(cells)
## # View(cells)
## ------------------------------------------------------------------------
range(cells$cell_area)
max(cells$gfp_T0)
which.max(cells$gfp_T0)
quantile(cells$gfp_T0)
quantile(cells$gfp_T0, probs = c(0.01, 0.25, 0.75, 0.99))
unique(cells$Well)
n_distinct(cells$Well)
## ------------------------------------------------------------------------
table(cells$Well)
table(cells$Well, cells$treated)
## ------------------------------------------------------------------------
mean(cells$roundness)
median(cells$roundness)
cor(cells$cell_area, cells$gfp_T1, method = "spearman")
cor.test(cells$cell_area, cells$gfp_T1, method = "spearman")
## ------------------------------------------------------------------------
# make sure that the ggplot2 package is actually loaded: library(ggplot) or
# library(tidyverse)
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0))
## ------------------------------------------------------------------------
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point()
## ------------------------------------------------------------------------
p <- ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point()
p
## ------------------------------------------------------------------------
p <- p + labs(x = "GFP at time point T0", y = "mCherry at time point T0",
title = "mCherry vs. GFP intensity")
p
## ------------------------------------------------------------------------
# units are in inches
ggsave(filename = "A_plot_has_been_born.pdf", plot = p, width = 5, height = 5)
## ------------------------------------------------------------------------
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point(aes(colour = treated)) +
labs(x = "GFP at time point T0", y = "mCherry at time point T0",
title = "mCherry vs. GFP intensity")
# any part of the plotting command can be saved in a variable:
my_label <- labs(x = "GFP at time point T0", y = "mCherry at time point T0",
title = "mCherry vs. GFP intensity")
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point(aes(colour = treated)) +
my_label
## ------------------------------------------------------------------------
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point(aes(colour = treated)) +
geom_smooth(method = "lm", se = FALSE) +
my_label
## ------------------------------------------------------------------------
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point(aes(colour = treated)) +
geom_smooth(aes(colour = treated), method = "lm", se = FALSE) +
my_label
# using inheritance
ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0, colour = treated)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
my_label
## ------------------------------------------------------------------------
cells %>%
filter(treated == "yes") %>%
lm(formula = mCh_T0 ~ gfp_T0)
group_by(cells, treated) %>%
summarise(my_mod = list(lm(mCh_T0 ~ gfp_T0)))
## ------------------------------------------------------------------------
ggplot(cells, aes(x = treated)) +
geom_bar()
cells[1:6, ] %>%
ggplot(aes(x = cellID, y = gfp_T0)) +
geom_bar(stat = "identity")
## ------------------------------------------------------------------------
ggplot(cells, aes(x = Well, y = gfp_T0)) +
geom_boxplot()
## ------------------------------------------------------------------------
ggplot(cells, aes(x = Well, y = gfp_T0)) +
geom_violin()
ggplot(cells, aes(x = Well, y = gfp_T0)) +
geom_violin() +
geom_point(alpha = 0.5)
## ------------------------------------------------------------------------
# set height to 0!
ggplot(cells, aes(x = Well, y = gfp_T0)) +
geom_violin() +
geom_point(alpha = 0.5, position = position_jitter(width = 0.1, height = 0))
## ------------------------------------------------------------------------
ggplot(cells, aes(x = Well, y = gfp_T0)) +
geom_violin() +
ggbeeswarm::geom_beeswarm(cex = 2)
## ------------------------------------------------------------------------
ggplot(cells, aes(x = gfp_T0)) +
geom_histogram(bins = 20)
## ------------------------------------------------------------------------
ggplot(cells, aes(x = gfp_T0)) +
geom_histogram(aes(y = ..density..), bins = 20)
## ------------------------------------------------------------------------
ggplot(cells, aes(x = gfp_T0)) +
geom_histogram(aes(y = ..density..), bins = 20) +
geom_density()
ggplot(cells, aes(x = gfp_T0)) +
geom_histogram(aes(y = ..density..), bins = 20) +
geom_density(adjust = 4)
## ------------------------------------------------------------------------
ggplot(cells, aes(x = gfp_T0)) +
geom_freqpoly(bins = 20)
## ------------------------------------------------------------------------
ggplot(cells, aes(x = gfp_T0, colour = Well)) +
stat_ecdf()
## ------------------------------------------------------------------------
p <- ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0))
p + geom_point(alpha = 0.2)
p + geom_point(shape = 1)
p + geom_point(aes(colour = treated)) +
geom_density_2d()
p + geom_point(aes(colour = treated)) +
geom_density_2d(h = 20)
p + stat_density_2d(geom = "polygon", aes(fill = ..level..))
p + stat_density_2d(geom = "polygon", aes(fill = ..level..))
p + geom_hex()
p + geom_hex(binwidth = c(20, 20))
## ------------------------------------------------------------------------
# before:
cells
# and after:
cells_tidy <- gather(cells, gfp_T0, mCh_T0, gfp_T1, gfp_T2, gfp_T3, mCh_T1,
mCh_T2, mCh_T3, key = channel_tpt, value = int)
cells_tidy
## ------------------------------------------------------------------------
cells_tidy <- separate(cells_tidy, col = channel_tpt,
into = c("channel", "tpt"), sep = "_")
cells_tidy
## ------------------------------------------------------------------------
cells_tidy <- filter(cells_tidy, channel == "gfp")
p <- ggplot(cells_tidy, aes(x = tpt, y = int, colour = treated))
## ------------------------------------------------------------------------
p + geom_line()
## ------------------------------------------------------------------------
p + geom_line(aes(group = cellID))
## ------------------------------------------------------------------------
(load("../data/mtec_counts.RData"))
mtec_counts
## ------------------------------------------------------------------------
p + geom_line(aes(group = cellID)) +
facet_wrap( ~ treated) # facet_grid also works
## ------------------------------------------------------------------------
p <- ggplot(data = cells, aes(x = gfp_T0, y = mCh_T0)) +
geom_point(aes(colour = treated))
p + scale_colour_manual("Treated?", labels = c("No", "Yes"),
values = c("black", "red"))
## ------------------------------------------------------------------------
# ggplot also accepts RGB and Hex codes:
p + scale_colour_manual("Treated?", labels = c("No", "Yes"),
values = c("#1b9e77", "#d95f02"))
## ------------------------------------------------------------------------
p
p + coord_cartesian(xlim = c(250, 300), ylim = c(200, 250)) +
geom_smooth(method = "lm", aes(colour = treated))
p + scale_x_continuous(limits = c(250, 300)) +
scale_y_continuous(limits = c(200, 250)) +
geom_smooth(method = "lm", aes(colour = treated))
## ------------------------------------------------------------------------
gfp_summary <-
group_by(cells, Well) %>%
summarise(mean_gfp = mean(gfp_T0),
lower = mean_gfp - sd(gfp_T0),
upper = mean_gfp + sd(gfp_T0))
ggplot(gfp_summary, aes(x = Well, y = mean_gfp)) +
geom_bar(stat = "identity")
## ------------------------------------------------------------------------
(f <- factor(gfp_summary$Well))
as.integer(f)
levels(f)
(f2 <- factor(gfp_summary$Well, levels = c("B1", "A1")))
as.integer(f2)
levels(f2)
gfp_summary$Well <- factor(gfp_summary$Well, levels = c("B1", "A1"))
ggplot(gfp_summary, aes(x = Well, y = mean_gfp)) +
geom_bar(stat = "identity")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment