【发布时间】: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 行的单独数据框,每个面板两个,一个“颜色”分组变量,然后是斜率和截距列。
-
谢谢。我在概念层面上得到了它,但我被困在执行中。
-
我会试着把一些东西放在一起......
-
非常感谢,我目前正在与来自
reshape2的melt合作。
标签: r ggplot2 legend reshape2 data-manipulation