I have some biological data for two individuals, and I graph it using R as a scatterplot using ggplot, like this:
p1<-ggplot(data, aes(meth_matrix$sample1, meth_matrix$sample3)) +
geom_point() +
theme_minimal()
Which works perfectly, but I want to add lines to it: the abline that divides the scatterplot in half:
p1 + geom_abline(color="blue")
And my question is: how can I draw two red lines parallel to that diagonal (y intercept would be 0.2, slope would be the same as the blue line) ??
Also, how can I draw the difference between the two samples in a similar scatterplot (it will look like a horizontal scatterplot) with ggplot? Right now, I can only do it with a plot like:
dif_samples<-meth_matrix$sample1- meth_matrix$sample3
plot(dif_samples, main="difference",
xlab="CpGs ", ylab="Methylation ", pch=19)
(Also, I'd like to add the horizontal blue line and the red lines parallel to the blue line.)
Please help!!!
Thank you very much.
You can specify slopes and intercepts in the geom_abline() function. I'll use the iris dataset that comes with ggplot2 to illustrate:
# I'll use the iris dataset. I normalise by dividing the variables by their max so that
# a line through the origin will be visible
library(ggplot2)
p1 <- ggplot(iris, aes(Sepal.Length/max(Sepal.Length), Sepal.Width/max(Sepal.Width))) +
geom_point() + theme_minimal()
# Draw lines by specifying their slopes and intercepts. since all lines
# share a slope I just give one insted of a vector of slopes
p1 + geom_abline(intercept = c(0, .2, -.2), slope = 1,
color = c("blue", "red", "red"))

I'm not as clear on exactly what you want for the second plot, but you can plot differences directly in the call to ggplot() and you can add horizontal lines with geom_hline():
# Now lets plot the difference between sepal length and width
# for each observation
p2 <- ggplot(iris, aes(x = 1:nrow(iris),
y = (Sepal.Length - Sepal.Width) )) +
geom_point() + theme_minimal()
# we'll add horizontal lines -- you can pick values that make sense for your problem
p2 + geom_hline(yintercept = c(3, 3.2, 2.8),
color = c("blue", "red", "red"))

Created on 2018-03-21 by the reprex package (v0.2.0).