Reputation: 55
I want to plot 2 variables from 3 different dataframes in one scatterplot and also plot the equations of each linear relationship automatically. I am using the following code. However I have two problems:
I get the plots for the same values and not for the whole range (e.g. df1 =700
values, df2= 350
values, df3=450
values). What is the role of omitting the NA? Because I tried that both ways and I still get the same plot
I can only add the equations as a text which means to run the lm function and then add the relathionship manually in the plot. I need to do that automatically.
The code that I am using is:
ggplot(df1, aes(x=noxppb, y=OX, colour = "red")) +
geom_point(colour = "red", shape=2) + # Use hollow circles
geom_smooth(method=lm, se = FALSE) +
geom_point(data = df1, aes(x=noxppb, y=OX)) +
geom_point(colour = "blue", shape=3) +
geom_smooth(method = lm, se = F, colour = "blue", data = df2, aes(x=noxppb, y=OX)) +
geom_point(colour = "green", shape=4) +
geom_smooth(method = lm, se = F, colour = "green", data = df3, aes(x=noxppb, y=OX))
However I Need something similar to this:
Upvotes: 0
Views: 1361
Reputation: 77116
try this,
d <- plyr::mdply(data.frame(a=c(1,2,3), b=c(-1,0,1)),
function(a,b) data.frame(x=seq(0,10), y=jitter(a*seq(0,10)+b)))
equationise = function(d, ...){
m = lm(y ~ x, d)
eq <- substitute(italic(y) == a + b %.% italic(x),
list(a = format(coef(m)[1], ...),
b = format(coef(m)[2], ...)))
data.frame(x = Inf, y = d$y[nrow(d)],
label = as.character(as.expression(eq)),
stringsAsFactors = FALSE)
}
eqs <- plyr::ddply(d, "a", equationise, digits = 2)
ggplot(d, aes(x=x, y=y, colour = factor(a))) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_label(data=eqs, aes(label = label), parse=TRUE, hjust=1)
Upvotes: 3