Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Last active November 2, 2016 15:36
Show Gist options
  • Save explodecomputer/8e245ffc37613a6dbb4adb8c2a95cbab to your computer and use it in GitHub Desktop.
Save explodecomputer/8e245ffc37613a6dbb4adb8c2a95cbab to your computer and use it in GitHub Desktop.
random graphs for laura
library(ggplot2)
library(reshape2)
library(dplyr)
library(latex2exp)
# Read in data
dat <- read.table("Downloads/data_for_figures.txt", he=T, sep=",")
# Need to make the pos columns in long format
d1 <- melt(subset(dat, select=c(binary_selection_total, average_pos1, average_pos2, average_pos3, average_pos4)), measure.vars=c("average_pos1", "average_pos2", "average_pos3", "average_pos4"))
# Set label values
d1$binary_selection_total <- as.factor(d1$binary_selection_total)
levels(d1$variable) <- c("Position 1", "Position 2", "Position 3", "Position 4")
# Plot box plots
ggplot(d1, aes(y=value, x=variable)) +
geom_boxplot(aes(fill=binary_selection_total)) +
labs(x="CpG position", y="Methylation %", fill="Selection Group") +
scale_fill_grey(start=0.4, end=0.75) +
ggsave("~/g1.pdf")
# scatter plot with linear models
ggplot(dat, aes(x=average_pos1, y=par4_integrin_ec50_clean)) +
stat_smooth(
method="lm",
alpha=0.15,
fullrange=TRUE,
aes(linetype=factor(binary_selection_total)),
colour="black"
) +
geom_point(aes(shape=factor(binary_selection_total))) +
labs(x="CpG position 1", colour="Selection\nGroup") +
scale_colour_grey(start=0.4, end=0.75)
ggsave("~/g2.pdf")
## boxplot of haematology
d3 <- subset(dat, select=c(binary_selection_total, wbc, rbc, plt, mpv)) %>%
melt(measure.vars=c("wbc", "rbc", "plt", "mpv"))
levels(d3$variable) <- c(
TeX("WBC ($10^3/mm^3$)"),
TeX("RBC ($10^6/mm^3$)"),
TeX("PLT ($10^3/mm^3$)"),
TeX("MPV ($\\mu m^3$)")
)
ggplot(d3, aes(x=variable, y=value, fill=factor(binary_selection_total))) +
geom_boxplot() +
scale_y_log10() +
labs(x="Haematology", y="Value", fill="Binary\nselection") +
theme(axis.text.x=element_text(angle=45, hjust=1, vjust=1)) +
scale_fill_grey(start=0.4, end=0.75) +
scale_x_discrete(labels=parse(text=levels(d3$variable)))
ggsave("~/g3.pdf")
# Bar charts
# This is an example of how to do the barchart of surface receptors
# But all the data is not available for those bars
# So just using the wbc/rbc/etc
geomean <- function(x, na.rm = FALSE, trim = 0, ...)
{
exp(mean(log(x, ...), na.rm = na.rm, trim = trim, ...))
}
geosd <- function(x, na.rm = FALSE, ...)
{
exp(sd(log(x, ...), na.rm = na.rm, ...))
}
# Need to make these long format also
# Calculate geometric mean and se for each bar
d2 <- subset(dat, select=c(binary_selection_total, wbc, rbc, plt, mpv)) %>%
melt(measure.vars=c("wbc", "rbc", "plt", "mpv")) %>%
dplyr::group_by(variable, binary_selection_total) %>%
dplyr::summarise(
m = geomean(value, na.rm=TRUE),
up = m + (geosd(value, na.rm=TRUE)) / sqrt(sum(!is.na(value))),
lo = m - (geosd(value, na.rm=TRUE)) / sqrt(sum(!is.na(value)))
)
levels(d2$variable) <- c(
TeX("WBC ($10^3/mm^3$)"),
TeX("RBC ($10^6/mm^3$)"),
TeX("PLT ($10^3/mm^3$)"),
TeX("MPV ($\\mu m^3$)")
)
ggplot(d2, aes(x=variable, y=m, group=factor(binary_selection_total))) +
geom_bar(stat="identity", position=position_dodge(), aes(fill=factor(binary_selection_total))) +
geom_errorbar(aes(ymin=lo, ymax=up), position=position_dodge(width=0.9), width=0) +
scale_y_log10() +
labs(x="Surface receptors", y="Geometric mean", fill="Binary\nselection") +
theme(axis.text.x=element_text(angle=45, hjust=1, vjust=1)) +
geom_text(aes(label=round(m, 1), y=1.1), position=position_dodge(width=0.9), size=3) +
scale_fill_grey(start=0.4, end=0.75) +
scale_x_discrete(labels=parse(text=levels(d2$variable)))
ggsave("~/g4.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment