在ggplot2中添加图例中的额外分组。

huangapple go评论60阅读模式
英文:

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 &lt;- rbind(c(1, 0.8), c(0.8, 1))
mu &lt;- c(4.5, 3.2)
dta1 &lt;- as.data.frame(
  mvrnorm(n = 1000, mu = mu, Sigma = sigma1)) |&gt; 
  mutate(
    group = as.factor(sample(c(1), 1000, replace = TRUE))
)

sigma2 &lt;- rbind(c(1, -0.5), c(-0.5, 1))
dta2 &lt;- as.data.frame(
  mvrnorm(n = 1000, mu = mu, Sigma = sigma2)) |&gt; 
  mutate(
  group = as.factor(sample(c(2), 1000, replace = TRUE))
)

dta &lt;- rbind(dta1, dta2)

#Create the graphic
ggplot(dta, aes(x = V1, y = V2)) +
  geom_point(aes(color = group)) +
  geom_smooth(method = &quot;lm&quot;, se = FALSE) +
  geom_smooth(method = &quot;lm&quot;, se = FALSE, aes(color = group)) +
  scale_color_manual(name = &quot;Legend&quot;, values = c(&quot;green&quot;, &quot;orange&quot;), labels = c(&quot;A&quot;, &quot;B&quot;))

答案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 &lt;- factor(dta$group,levels = c(&#39;1&#39;,&#39;2&#39;,&#39;3&#39;))

ggplot(dta, aes(x = V1, y = V2)) +
  geom_point(aes(color = group)) +
  geom_smooth(method = &quot;lm&quot;, se = FALSE) +
  geom_smooth(method = &quot;lm&quot;, se = FALSE, aes(color = group)) +
  scale_color_manual(name = &quot;Legend&quot;, 
                     values = c(&quot;green&quot;, &quot;orange&quot;,&quot;blue&quot;), 
                     labels = c(&quot;A&quot;, &quot;B&quot;,&quot;Overall&quot;),
                     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.

huangapple
  • 本文由 发表于 2023年3月4日 03:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定