Good geom_text quality in multiple facet_grid in ggplot2 with regression equations
<p>I wanted to plot several regressions equations in different facets using ggplot2...and I think I succeeded thanks to <a href="https://stackoverflow.com/questions/28162486/display-regression-slopes-for-multiple-subsets-in-ggplot2-facet-grid">this post</a>.</p>

<p>But geom_text font is ugly, as it copies the same letters several times in the same text zone :</p>

<pre><code>iris &lt;- iris
iris2 &lt;- data.frame(iris, family = c(rep(rep(1:2,25),3)))
iris3 &lt;- data.frame(iris2, group = paste(iris2$Species, iris2$family, sep = "_"))

intercept &lt;- ddply(iris3, .(group), function(x)  coefficients(lm(Petal.Length~Petal.Width,x)))
rcarre &lt;- ddply(iris3, .(group), function(x) summary(lm(Petal.Length~Petal.Width,x))$r.squared) 

names(rcarre) &lt;- c("group", "R2") 
names(intercept) &lt;- c("group", "intercept", "slope") 
coefs &lt;- merge(intercept, rcarre) 
coefs &lt;- data.frame(coefs,
                eq = paste("Y=",round(coefs$intercept,2),"+",round(coefs$slope, 2),"x",", ","R2=",round(coefs$R2,2), sep = ""))
coefs$eq = as.character(coefs$eq)

iris4 &lt;- merge(iris3, coefs)

ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) +
  geom_point() +
  geom_smooth(method=lm, se=F) +
  facet_grid(family~Species) +
  geom_text(aes(label=eq, x=1.5, y=6)) +
  theme_linedraw() 
</code></pre>

<p>I tried to use the given solution <a href="https://stackoverflow.com/questions/10952832/ggplot2-is-there-a-fix-for-jagged-poor-quality-text-produced-by-geom-text">here</a> but it doesn't work for me</p>

<pre><code>ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) +
  geom_point() +
  geom_smooth(method=lm, se=F) +
  facet_grid(family~Species) +
  geom_text(aes(label=iris4$eq, x=1.5, y=6), data = data.frame()) +
  theme_linedraw() 
</code></pre>

<p>With annotate, I have an error message (I understand the problem but I can't solve it)</p>

<pre><code>ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) +
  geom_point() +
  geom_smooth(method=lm, se=F) +
  facet_grid(family~Species) +
  annotate("text", x = 1.5, y = 6, label = iris4$eq) +
  theme_linedraw() 
</code></pre>

<p>And if I refer to the coefs table (same length than the facets), the equations don't match anymore</p>

<pre><code>ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) +
  geom_point() +
  geom_smooth(method=lm, se=F) +
  facet_grid(family~Species) +
  annotate("text", x = 1.5, y = 6, label = coefs$eq) +
  theme_linedraw() 
</code></pre>

<p>Anyone would have a solution ? </p>

<p>Thank you very much !</p>