3
votes

I have two questions:
- how to have the same color in geom_text as in the geom_point?
- how to remove legend title?

Assuming the following data set:

library(ggplot2)
library(reshape2)

speed <- c(0, seq(from = 30, to = 330, by = 60))
power1 <- c(75, 85, 88, 85, 94, 92, 95)
power2 <- c(72, 82, 78, 69, 74, 85, 89)
dt <- data.frame(speed=speed, power1=power1, power2=power2)
dt <- melt(data = dt, id.vars = c("speed"))

I'm making the plot with the following code:

p <- ggplot(data = dt, aes(x = speed, y = value, fill = variable)) + 
    geom_point(aes(color=variable), size = 2) +
    stat_smooth(aes(color=variable), method = lm, formula = y ~ poly(x, 2), se = FALSE, size = 1) 

p <- p + geom_text(aes(x = 100, 
                       y = 93, 
                       label = lm_eqn(lm(formula = value ~ poly(speed,2), 
                                         data = subset(x = dt, subset = variable == "power1")
                                         )
                                      ) 
                       ),
                   color = "black",
                   parse = TRUE) +
    geom_text(aes(x = 250, 
                  y = 72, 
                  label = lm_eqn(lm(formula = value ~ poly(speed,2), 
                                    data = subset(x = dt, subset = variable == "power2")
                                    )
                                 )
                  ), 
              color = "black", 
              parse = TRUE)

p

Help function for the label:

lm_eqn = function(m) {
    # from: 
    # https://stackguides.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph

    l <- list(a = format(coef(m)[1], digits = 2),
              b = format(abs(coef(m)[2]), digits = 2),
              c = format(abs(coef(m)[3]), digits = 2),
              s1 = ifelse(test = coef(m)[2]>0, yes = "+", no = "-"),
              s2 = ifelse(test = coef(m)[3]>0, yes = "+", no = "-"),
              r2 = format(summary(m)$r.squared, digits = 3));

    eq <- substitute(italic(y) == a~~s1~~b %.% italic(x)^2 ~~s2~~c%.% italic(x)*","~~italic(r)^2~"="~r2,l)

    as.character(as.expression(eq));                 
}

And the plot looks like this:

ggplot2 plot

So, how to:
- change geom_text color to the same as points color?
- remove legend title?

1

1 Answers

3
votes

First, simply add color = "power1" and color = "power2" to the respective geom_text() calls. This will set the appropriate color, but give you a nasty (but expected) "a" in your legend. To remove the "a" text, add show_guide=FALSE to each of the geom_text() calls.

Second, I believe you want the first aes call to have group = variable not fill = variable. This will help with confusion when you try to remove the title from the legend. To remove the title from the legend, add guides(color = guide_lengend(title=NULL)).

The code:

p <- ggplot(data = dt, aes(x = speed, y = value, group = variable)) + 
  geom_point(aes(color=variable), size = 2) +
  stat_smooth(aes(color=variable), method = lm, formula = y ~ poly(x, 2), se = FALSE, size = 1) +
  geom_text(aes(x=100, y=93, label="First Label", color="power1"), show_guide=F) +
  geom_text(aes(x=250, y=72, label="Second Label", color="power2"), show_guide=F) +
  guides(color = guide_legend(title=NULL))
p

The result:

Result

Note: You will want to be careful with overplotting that results with geom_text() - see this post and this answer for more information and ideas.