我想在我的情节中添加3条回归线:一条用于我的所有数据,另外两条用于子组。但是子组图例(gr1& gr2)在图例中显示为color
的值。
set.seed(18)
data <- data.frame(
x = sample(seq(from=0, to=4, by=0.1), 100, replace=T),
y = sample(seq(from=0, to=4, by=0.15), 100, replace=T),
severity = sample.int(20, 100, replace=T),
group = sample(c("gr1", "gr2"), 100, replace=T))
这是我的情节设置方式:
ggplot() +
geom_point(data=data, aes(x=x, y=y, color=as.factor(severity)))+
geom_smooth(method="lm", data=data, aes(x, y)) +
geom_smooth(method="lm", data=data, aes(x, y, color=group)) +
labs(x="x", y="y", color="title")
我希望将子组作为单独的解释性图例(与color=as.factor(severity)
的图例分开),最好是预先确定的颜色。任何帮助将不胜感激。
答案 0 :(得分:0)
虚拟解决方案可能就像这样
set.seed(18)
data <- data.frame(
x = sample(seq(from=0, to=4, by=0.1), 100, replace=T),
y = sample(seq(from=0, to=4, by=0.15), 100, replace=T),
severity = sample.int(20, 100, replace=T),
group = sample(c("gr1", "gr2"), 100, replace=T))
ggplot() +
geom_smooth(method="lm", data=data, aes(x, y), lty = 2, col = "black") +
geom_smooth(method="lm", data=data, aes(x, y, fill=group, lty = group), se = F) +
labs(x="x", y="y", color="title") +
geom_point(data=data, aes(x=x, y=y, color=as.factor(severity)))
答案 1 :(得分:0)
您要为同一色标添加更多值,以便它们显示在同一图例中。我的解决方案是首先复制数据,为整个数据添加另一个组,以便一个geom_smooth()
足够,并使用fill
geom_point()
,shape
允许library(tidyverse)
set.seed(18)
data <- data.frame(
x = sample(seq(from=0, to=4, by=0.1), 100, replace=T),
y = sample(seq(from=0, to=4, by=0.15), 100, replace=T),
severity = sample.int(20, 100, replace=T),
group = sample(c("gr1", "gr2"), 100, replace=T))
data2 <- data %>%
as_tibble() %>% # I like tibbles
mutate(group = "gr_all") %>%
bind_rows(data)
data %>%
ggplot(aes(x, y)) +
geom_point(aes(fill = severity %>% as.factor()), shape = 21) +
geom_smooth(data = data2, aes(color = group), method = "lm", se = FALSE) +
scale_color_manual(values = c("blue", "gray10", "gray5")) +
labs(x = "x", y = "y", fill = "title")
填写以获得单独的传奇。
{{1}}