Reputation: 6874
I have a plot of samples versus an number
the red dots have been modelled with a red regression line and the blue dots with a blue regression line. I would like to compare the two regression slopes to see if they are significantly different. I believe this is done with an ANCOVA. Does anyone know how to implement this in R?
The data to perform the graph is as follows:
structure(list(code = structure(c(25L, 37L, 13L,
29L, 17L, 10L, 24L, 20L, 38L, 35L, 32L, 28L, 26L, 39L, 21L, 15L,
31L, 9L, 16L, 23L, 7L, 18L, 42L, 34L, 4L, 33L, 19L, 22L, 27L), .Label = c("LP6005500-DNA_D01",
"LP6005334-DNA_E02", "LP6005334-DNA_G03", "LP6007427-DNA_A01",
"LP6005935-DNA_C03", "LP2000104-DNA_A01", "LP6005690-DNA_D01",
"LP6005409-DNA_C02", "LP6005500-DNA_D03", "LP6005334-DNA_D03",
"LP6005334-DNA_D01", "LP6007514-DNA_A01", "LP6005334-DNA_B02",
"LP6005334-DNA_F03", "LP6005500-DNA_B01", "LP6005500-DNA_E01",
"LP6005334-DNA_C03", "LP6005690-DNA_H01", "LP6007538-DNA_A01",
"LP6005334-DNA_E03", "LP6005500-DNA_A01", "LP6007540-DNA_A01",
"LP6005500-DNA_F01", "LP6005334-DNA_E01", "LP6005334-DNA_A02",
"LP6005409-DNA_A03", "LP6007542-DNA_A01", "LP6005334-DNA_H03",
"LP6005334-DNA_C02", "LP6007409-DNA_A01", "LP6005500-DNA_C01",
"LP6005334-DNA_H01", "LP6007512-DNA_A01", "LP6007424-DNA_A01",
"LP6005334-DNA_G02", "LP6005334-DNA_C01", "LP6005334-DNA_A03",
"LP6005334-DNA_F01", "LP6005409-DNA_C04", "LP6005334-DNA_D02",
"LP6007418-DNA_A02", "LP6007396-DNA_A01", "LP6005334-DNA_F02"
), class = "factor"), freq = c(503, 597, 354, 522, 399, 338,
498, 430, 606, 590, 561, 518, 508, 618, 436, 373, 559, 328, 382,
491, 313, 408, 683, 585, 261, 570, 423, 477, 515), CNI = c(21L,
54L, 25L, 32L, 19L, 23L, 21L, 18L, 25L, 29L, 32L, 27L, 37L, 49L,
26L, 11L, 11L, 24L, 13L, 31L, 19L, 21L, 28L, 32L, 17L, 44L, 22L,
20L, 15L)), .Names = c("code", "freq", "CNI"), row.names = c(1L,
2L, 3L, 5L, 6L, 9L, 10L, 12L, 13L, 16L, 18L, 19L, 20L, 22L, 23L,
24L, 25L, 27L, 28L, 29L, 30L, 31L, 33L, 35L, 36L, 37L, 39L, 40L,
41L), class = "data.frame")
Here is the code for the graph
ggplot(FeqAndASCATmergeCell) +
geom_bar(aes(code,Cellul*100),stat="identity")+
geom_point(aes(code,freq),colour="blue")+
geom_smooth(aes(code,freq,group=2),fill = "blue",alpha=0.2)+
geom_point(aes(code,CNI*20),colour="red",size=5)+
geom_smooth(aes(code,CNI*20,group=2),fill = "red", colour="red", alpha=0.2)+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14))+
theme(legend.title=element_blank())+
theme(legend.position = c(0.7, 0.7))+
theme(axis.text.x=element_text(angle=-90))+
scale_y_continuous(breaks=c(25, 50,75, 100,300,600,900))+
theme(axis.text.y=element_text( size=10)) +
theme(legend.position = "none")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(plot.title = element_text(size=18,lineheight=.8, face="bold",vjust=1))+
theme(axis.text.x=element_text(angle=-90, size=10))
Upvotes: 0
Views: 396
Reputation: 3862
You can compare the slopes of a regression line with an ANCOVA, but only for straight lines, so with a constant slope. Because you didn't supply a method to geom_smooth
, it uses loess
and your lines don't have a constant slope. You can for example use method = lm
:
ggplot(FeqAndASCATmergeCell) +
geom_point(aes(code,freq),colour="blue")+
geom_smooth(aes(code,freq,group=2),fill = "blue",alpha=0.2, method="lm")+
geom_point(aes(code,CNI*20),colour="red",size=5)+
geom_smooth(aes(code,CNI*20,group=2),fill = "red", colour="red", alpha=0.2, method="lm")+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14))+
theme(legend.title=element_blank())+
theme(legend.position = c(0.7, 0.7))+
theme(axis.text.x=element_text(angle=-90))+
scale_y_continuous(breaks=c(25, 50,75, 100,300,600,900))+
theme(axis.text.y=element_text( size=10)) +
theme(legend.position = "none")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(plot.title = element_text(size=18,lineheight=.8, face="bold",vjust=1))+
theme(axis.text.x=element_text(angle=-90, size=10))
This will give you straight lines:
Next, you can use an ANCOVA to test if these lines are different. Basically you test if the slope or intercept of the lines significantly differ. How you do that would make too long of an answer, but see here for an R-example.
Upvotes: 1