【问题标题】:combining my two `geom_abline` in ggplot2 to get legend在 ggplot2 中结合我的两个“geom_abline”来获得图例
【发布时间】:2016-04-04 19:00:21
【问题描述】:

我有下面的情节,使用此代码(请参阅下面的数据),但我正在努力在aes 调用中获得group 以获得有意义的图例(黑色和蓝色线) .我正在尝试以某种方式将两个geom_abline 结合起来。任何帮助将不胜感激,谢谢!

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
p1 <- ggplot(dfn, aes(x, y)) +
     geom_jitter(position = position_jitter(width = .05, height = 0)) + 
     scale_x_continuous(breaks=c(0,1), labels=c("0", "1")) +
     geom_abline(aes(intercept = in.a, slope = sl.a)) +
     geom_abline(aes(intercept = in.b, slope = sl.b), colour="blue") +    
     facet_wrap(~ group, ncol = 2)
p1

## data
dfn <- data.frame(
x = sample(c(0,1), 91, replace = TRUE),
y = rnorm(91, mean = 1, sd = 2),
in.a = rep(1.31, 91),
sl.a = rep(-0.61, 91),
in.b = c(0.62, 0.62, 0.62, 0.62, 0.90, 0.90, 0.90, 0.90, 
0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 
0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 
0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 
0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 
0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 
0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 0.90, 
1.41, 1.41, 1.41, 1.68, 1.68, 1.68, 1.68, 1.68, 
1.68, 1.68, 1.29, 1.29, 1.29, 1.29, 1.49, 1.49, 
1.49, 1.85, 1.85, 1.85, 1.85, 1.85, 1.85, 1.85, 
1.85, 1.85, 1.85, 1.85, 1.85, 1.85, 1.85, 2.08, 
2.08, 2.08, 2.08), sl.b = 
c(0.18, 0.18, 0.18, 0.18, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-1.20, -1.20, -1.20, -1.20, -1.20, -1.20, -1.20, 
-0.54, -0.54, -0.54, -0.97, -0.97, -0.97, -0.97, 
-0.97, -0.97, -0.97, -0.22, -0.22, -0.22, -0.22, 
0.06, 0.06, 0.06, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, -0.93, -0.93, -0.93, -0.93
),
group = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L)
)

【问题讨论】:

  • 您的问题是您试图将 abline 信息粉碎到与其余数据相同的数据框中。您需要一个包含 16 行的单独数据框,每个面板两个,一个“颜色”分组变量,然后是斜率和截距列。
  • 谢谢。我在概念层面上得到了它,但我被困在执行中。
  • 我会试着把一些东西放在一起......
  • 非常感谢,我目前正在与来自reshape2melt 合作。

标签: r ggplot2 legend reshape2 data-manipulation


【解决方案1】:

所以如果你使用 reshape2,我会做这样的事情:

library(reshape2)
#Starting point
ab_dat <- unique(dfn[,-(1:2)])

#Melt the slopes and intercepts separately and then join
ab_dat1 <- melt(ab_dat,
                id.vars = c('group'),
                measure.vars = c('in.a','in.b'),
                variable.name = 'color_grp',
                value.name = 'intercept')
ab_dat1$color_grp <- gsub("in.","",ab_dat1$color_grp,fixed = TRUE)

ab_dat2 <- melt(ab_dat,
                id.vars = c('group'),
                measure.vars = c('sl.a','sl.b'),
                variable.name = 'color_grp',
                value.name = 'slope')
ab_dat2$color_grp <- gsub("sl.","",ab_dat1$color_grp,fixed = TRUE)

ab_dat3 <- merge(ab_dat1,ab_dat2)

library(ggplot2)
p1 <- ggplot() +
    facet_wrap(~ group, ncol = 2) +
    geom_jitter(data = dfn, aes(x, y),
                position = position_jitter(width = .05, height = 0)) + 
    scale_x_continuous(breaks=c(0,1), labels=c("0", "1")) +
    geom_abline(data = ab_dat3,
                aes(intercept = intercept, slope = slope,color = color_grp))
p1

【讨论】:

  • 哇,非常感谢。我没想到我需要这么多行代码来获得一个图例。无论如何,我非常感谢您的帮助!
  • @EricFail 好吧,在你做了几次这种事情之后,你知道将 abline 数据放在一个单独的数据框中,事情就简单多了。使用 dplyr 和 tidyr 可以用更少的代码行来完成,但可能差别不大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
相关资源
最近更新 更多