Reputation: 592
I have made a plot in ggplot2 using geom_point. I want to fit a regression line to the infected and uninfected expression values, denoted as y and n respectively. How can I do that?
#plotting
p = ggplot(oas, aes(x=hpi,y=expression, color = infection_status) ) +
geom_point()
p + labs(x = "Hours post infection", y = "Expression (log2cpm +1)", colour = "Infected?")
The data frame used to make the plot:
> print(oas)
expression hpi infection_status
SRR1346026 -1.99296894 4 N
SRR1346027 -2.73036339 4 Y
SRR1346028 -2.09282698 4 Y
SRR1346029 -3.20461929 4 Y
SRR1346030 -2.17659914 6 N
SRR1346031 -0.79651276 6 Y
SRR1346032 0.07630312 6 Y
SRR1346033 -2.16659057 6 Y
SRR1346034 -1.60442152 12 N
SRR1346035 1.77516608 12 Y
SRR1346036 3.37072286 12 Y
SRR1346037 -1.03317733 24 N
SRR1346038 6.20459628 24 Y
SRR1346039 7.38838203 24 Y
SRR1346040 5.64524296 24 Y
SRR1346041 -2.61149329 48 N
SRR1346042 -0.62253394 48 N
SRR1346043 -1.22872678 48 N
SRR1346044 3.48661571 48 Y
SRR1346045 5.14172576 48 Y
SRR1346046 8.00577520 48 Y
SRR1346047 8.78170903 48 Y
SRR1346049 -0.05784844 72 N
SRR1346048 -1.30151097 72 N
SRR1346050 8.77407666 72 Y
SRR1346051 9.18594576 72 Y
SRR1346052 8.76202212 72 Y
Upvotes: 1
Views: 588
Reputation: 79204
You can use geom_smooth
. Simply use this line:
p + geom_smooth(method = "lm", se = FALSE)
Upvotes: 1
Reputation: 11128
Are you looking for this:
library(ggplot2)
library(ggpubr) ## for getting stat_regline_equation
p = ggplot(oas, aes(x=hpi,y=expression, color = infection_status) ) + geom_point()
p + labs(x = "Hours post infection", y = "Expression (log2cpm +1)") + stat_smooth(se=FALSE,aes(color = infection_status), method = "lm") + stat_regline_equation()
Upvotes: 0