Reputation: 311
I have a df:
MixA<-c(11.3296500, 3.7765500, 13.2179250, 1.8882750, 60.4248063, 906.3720938, 483.3984500, 1450.1953500, 0.4130875, 0.0590125)
TPM<-c(496.510998, 121.020332, 32.194033, 0.584421, 63.569152, 253.681165, 0.000000, 30487.460466, 0.000000, 0.000000)
test<-data.frame(MixA,TPM)
I want to log so I arbitarily replace the zeros and I want to plot these 'zero' points in a seperate colour so I group the data:
test[test == 0]<-0.01
test$group<-as.factor(ifelse(test$TPM==1.000000e-02,0,1))
When I plot with ggplot and it plots a lm for both groups but I want to remove the lm for the 'zero' group.
ggplot(test, aes(x=log10(test$MixA),y=log10(test$TPM),color=group)) + geom_point(size=3) +
geom_smooth(method=lm) + scale_x_continuous(limits=c(-2.5, 3)) + scale_y_continuous(limits=c(-2.5, 5)) +
theme(legend.position = "none")
I tried adding aes(group=1) to geom_smooth but this seems to shift the whole line??:
ggplot(test, aes(x=log10(test$MixA),y=log10(test$TPM),color=group)) + geom_point(size=3) +
scale_x_continuous(limits=c(-2.5, 3)) + scale_y_continuous(limits=c(-2.5, 5)) +geom_smooth(aes(group=1), method="lm")+
theme(legend.position = "none")
Upvotes: 0
Views: 746
Reputation: 140
You can modify your geom_smooth()
statement to reflect the groups for which you wish to plot a line.
ggplot(data = test, aes(x=log10(MixA),y=log10(TPM),color=group)) + geom_point(size=3) +
geom_smooth(data = subset(test, group == 1), aes(x= log10(MixA),y= log10(TPM),color=group), method=lm) +
scale_x_continuous(limits=c(-2.5, 3)) +
scale_y_continuous(limits=c(-2.5, 5)) +
theme(legend.position = "none")
This should give you the plot you want.
Upvotes: 1