将geom_text颜色与绘图中的元素匹配,并在ggplot2中删除图例标题

时间:2015-01-13 15:06:06

标签: r ggplot2 geom-text

我有两个问题:
- 如何在geom_text中使用与geom_point相同的颜色?
- 如何删除图例标题?

假设以下数据集:

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"))

我使用以下代码制作情节:

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

标签的帮助功能:

lm_eqn = function(m) {
    # from: 
    # https://stackoverflow.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));                 
}

情节如下:

ggplot2 plot

那么,如何:
- 将geom_text颜色更改为与颜色相同的颜色?
- 删除图例标题?

1 个答案:

答案 0 :(得分:3)

首先,只需将color = "power1"color = "power2"添加到相应的geom_text()来电。这将设置适当的颜色,但给你一个令人讨厌的(但预期的)&#34; a&#34;在你的传奇中。删除&#34; a&#34;文字,为每个show_guide=FALSE来电添加geom_text()

其次,我相信您希望第一次aes调用group = variable 而不是 fill = variable。当您尝试从图例中删除标题时,这将有助于混淆。要从图例中删除标题,请添加guides(color = guide_lengend(title=NULL))

代码:

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

结果:

Result

注意:您需要小心使用geom_text()对结果进行过度绘制 - 请参阅此post和此answer以获取更多信息和想法。