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

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

Add additional group in legend in ggplot2

问题

我正在尝试创建一个回归图,显示两个子组的回归线,以及整个数据框。

在这个过程中,我遇到了一个问题,即是否可以将一个在数据框中不存在的组添加到图例中(我的变量只有两个不同的组,但我想在图例中写入三个内容)。

对我来说,特别是为了添加一个合并了两个组的回归的图例。但我也在一般情况下进行思考。
以下是一些示例代码。

每一点帮助都非常感激!

  1. #加载包
  2. library(MASS)
  3. library(ggplot2)
  4. library(dplyr)
  5. #设置种子
  6. set.seed(1234)
  7. #创建随机数据框
  8. sigma1 <- rbind(c(1, 0.8), c(0.8, 1))
  9. mu <- c(4.5, 3.2)
  10. dta1 <- as.data.frame(
  11. mvrnorm(n = 1000, mu = mu, Sigma = sigma1)) %>%
  12. mutate(
  13. group = as.factor(sample(c(1), 1000, replace = TRUE))
  14. )
  15. sigma2 <- rbind(c(1, -0.5), c(-0.5, 1))
  16. dta2 <- as.data.frame(
  17. mvrnorm(n = 1000, mu = mu, Sigma = sigma2)) %>%
  18. mutate(
  19. group = as.factor(sample(c(2), 1000, replace = TRUE))
  20. )
  21. dta <- rbind(dta1, dta2)
  22. #创建图形
  23. ggplot(dta, aes(x = V1, y = V2)) +
  24. geom_point(aes(color = group)) +
  25. geom_smooth(method = "lm", se = FALSE) +
  26. geom_smooth(method = "lm", se = FALSE, aes(color = group)) +
  27. 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!

  1. #Load packages
  2. library(MASS)
  3. library(ggplot2)
  4. library(dplyr)
  5. #Set a seed
  6. set.seed(1234)
  7. #Create random dataframe
  8. sigma1 &lt;- rbind(c(1, 0.8), c(0.8, 1))
  9. mu &lt;- c(4.5, 3.2)
  10. dta1 &lt;- as.data.frame(
  11. mvrnorm(n = 1000, mu = mu, Sigma = sigma1)) |&gt;
  12. mutate(
  13. group = as.factor(sample(c(1), 1000, replace = TRUE))
  14. )
  15. sigma2 &lt;- rbind(c(1, -0.5), c(-0.5, 1))
  16. dta2 &lt;- as.data.frame(
  17. mvrnorm(n = 1000, mu = mu, Sigma = sigma2)) |&gt;
  18. mutate(
  19. group = as.factor(sample(c(2), 1000, replace = TRUE))
  20. )
  21. dta &lt;- rbind(dta1, dta2)
  22. #Create the graphic
  23. ggplot(dta, aes(x = V1, y = V2)) +
  24. geom_point(aes(color = group)) +
  25. geom_smooth(method = &quot;lm&quot;, se = FALSE) +
  26. geom_smooth(method = &quot;lm&quot;, se = FALSE, aes(color = group)) +
  27. 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

尝试这个:

  1. dta$group <- factor(dta$group, levels = c('1', '2', '3'))
  2. ggplot(dta, aes(x = V1, y = V2)) +
  3. geom_point(aes(color = group)) +
  4. geom_smooth(method = "lm", se = FALSE) +
  5. geom_smooth(method = "lm", se = FALSE, aes(color = group)) +
  6. scale_color_manual(name = "Legend",
  7. values = c("green", "orange", "blue"),
  8. labels = c("A", "B", "Overall"),
  9. drop = FALSE)

策略是创建一个未使用的“虚拟”因子水平,然后手动标记它为你想要的方式。请注意,在比例尺中需要包含 drop = FALSE,否则未使用的因子水平将被省略。

英文:

Try this:

  1. dta$group &lt;- factor(dta$group,levels = c(&#39;1&#39;,&#39;2&#39;,&#39;3&#39;))
  2. ggplot(dta, aes(x = V1, y = V2)) +
  3. geom_point(aes(color = group)) +
  4. geom_smooth(method = &quot;lm&quot;, se = FALSE) +
  5. geom_smooth(method = &quot;lm&quot;, se = FALSE, aes(color = group)) +
  6. scale_color_manual(name = &quot;Legend&quot;,
  7. values = c(&quot;green&quot;, &quot;orange&quot;,&quot;blue&quot;),
  8. labels = c(&quot;A&quot;, &quot;B&quot;,&quot;Overall&quot;),
  9. 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:

确定