英文:
Add additional group in legend in ggplot2
问题
我正在尝试创建一个回归图,显示两个子组的回归线,以及整个数据框。
在这个过程中,我遇到了一个问题,即是否可以将一个在数据框中不存在的组添加到图例中(我的变量只有两个不同的组,但我想在图例中写入三个内容)。
对我来说,特别是为了添加一个合并了两个组的回归的图例。但我也在一般情况下进行思考。
以下是一些示例代码。
每一点帮助都非常感激!
#加载包
library(MASS)
library(ggplot2)
library(dplyr)
#设置种子
set.seed(1234)
#创建随机数据框
sigma1 <- rbind(c(1, 0.8), c(0.8, 1))
mu <- c(4.5, 3.2)
dta1 <- as.data.frame(
mvrnorm(n = 1000, mu = mu, Sigma = sigma1)) %>%
mutate(
group = as.factor(sample(c(1), 1000, replace = TRUE))
)
sigma2 <- rbind(c(1, -0.5), c(-0.5, 1))
dta2 <- as.data.frame(
mvrnorm(n = 1000, mu = mu, Sigma = sigma2)) %>%
mutate(
group = as.factor(sample(c(2), 1000, replace = TRUE))
)
dta <- rbind(dta1, dta2)
#创建图形
ggplot(dta, aes(x = V1, y = V2)) +
geom_point(aes(color = group)) +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(method = "lm", se = FALSE, aes(color = group)) +
scale_color_manual(name = "图例", values = c("green", "orange"), labels = c("A", "B"))
希望对你有所帮助!
英文:
I was trying to create a regression plot that shows the regression line for two subgroups and also the entire dataframe.
While doing that i stumbled across the question if it was possible to add a group to the that doesn't exist in the dataframe to the legend (my variable only has two distinct groups, but I want to write three things in the legend).
For me specifically to add a legend for the regression with both groups combined. But I was also wondering in general.
Below you find some sample code.
Every help is much appreciated!
#Load packages
library(MASS)
library(ggplot2)
library(dplyr)
#Set a seed
set.seed(1234)
#Create random dataframe
sigma1 <- rbind(c(1, 0.8), c(0.8, 1))
mu <- c(4.5, 3.2)
dta1 <- as.data.frame(
mvrnorm(n = 1000, mu = mu, Sigma = sigma1)) |>
mutate(
group = as.factor(sample(c(1), 1000, replace = TRUE))
)
sigma2 <- rbind(c(1, -0.5), c(-0.5, 1))
dta2 <- as.data.frame(
mvrnorm(n = 1000, mu = mu, Sigma = sigma2)) |>
mutate(
group = as.factor(sample(c(2), 1000, replace = TRUE))
)
dta <- rbind(dta1, dta2)
#Create the graphic
ggplot(dta, aes(x = V1, y = V2)) +
geom_point(aes(color = group)) +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(method = "lm", se = FALSE, aes(color = group)) +
scale_color_manual(name = "Legend", values = c("green", "orange"), labels = c("A", "B"))
答案1
得分: 2
尝试这个:
dta$group <- factor(dta$group, levels = c('1', '2', '3'))
ggplot(dta, aes(x = V1, y = V2)) +
geom_point(aes(color = group)) +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(method = "lm", se = FALSE, aes(color = group)) +
scale_color_manual(name = "Legend",
values = c("green", "orange", "blue"),
labels = c("A", "B", "Overall"),
drop = FALSE)
策略是创建一个未使用的“虚拟”因子水平,然后手动标记它为你想要的方式。请注意,在比例尺中需要包含 drop = FALSE
,否则未使用的因子水平将被省略。
英文:
Try this:
dta$group <- factor(dta$group,levels = c('1','2','3'))
ggplot(dta, aes(x = V1, y = V2)) +
geom_point(aes(color = group)) +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(method = "lm", se = FALSE, aes(color = group)) +
scale_color_manual(name = "Legend",
values = c("green", "orange","blue"),
labels = c("A", "B","Overall"),
drop = FALSE)
The strategy is to create a "dummy" unused factor level, and then manually label it the way you want. Note the need to include drop = FALSE
in the scale, otherwise the unused factor level will be omitted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论