如何将两个ggplot2自定义函数合并,使图例分开。

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

How can I combine two ggplot2 custom functions makig the legend separate

问题

我有两个自定义函数:

  1. plt_regression_line <- function(color = "black", linetype = "dashed"){
  2. list(
  3. ggplot2::geom_smooth(
  4. method = "lm",
  5. se = FALSE,
  6. formula = "y ~ x",
  7. mapping = ggplot2::aes(alpha = "回归线"),
  8. linetype = linetype,
  9. color = color),
  10. ggplot2::labs(alpha = ""),
  11. ggplot2::scale_alpha_manual(values = c(1,1))
  12. )
  13. }
  14. plt_identity_line <- function(color = "black", linetype = "dashed"){
  15. list(
  16. ggplot2::geom_abline(
  17. mapping = ggplot2::aes(slope = 1, intercept = 0, alpha = "一致性线"),
  18. linetype = linetype,
  19. color = color),
  20. ggplot2::labs(alpha = ""),
  21. ggplot2::scale_alpha_manual(values = c(1,1))
  22. )
  23. }

它们单独使用时都能正常工作,但当我将它们组合在一起时,图例无法区分它们:

  1. library(ggplot2)
  2. ggplot(mtcars,aes(qsec,mpg))+
  3. geom_point()+
  4. plt_regression_line(color = "red", linetype = 1)+
  5. plt_identity_line(color = "blue", linetype = 1)

如何将两个ggplot2自定义函数合并,使图例分开。

*我使用了 alpha,因为 colfill 可以用于其他变量,并且我希望在图例中显示它们。

英文:

I have two custom functions:

  1. plt_regression_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  2. list(
  3. ggplot2::geom_smooth(
  4. method = &quot;lm&quot;,
  5. se = FALSE,
  6. formula = &quot;y ~ x&quot;,
  7. mapping = ggplot2::aes(alpha = &quot;Regression line&quot;),
  8. linetype = linetype,
  9. color = color),
  10. ggplot2::labs(alpha = &quot;&quot;),
  11. ggplot2::scale_alpha_manual(values = c(1,1))
  12. )
  13. }

and

  1. plt_identity_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  2. list(
  3. ggplot2::geom_abline(
  4. mapping = ggplot2::aes(slope = 1, intercept = 0,alpha = &quot;Identity line&quot;),
  5. linetype = linetype,
  6. color = color),
  7. ggplot2::labs(alpha = &quot;&quot;),
  8. ggplot2::scale_alpha_manual(values = c(1,1))
  9. )
  10. }

They work perfect individually, but when I combine them the legend does not distinguish on from another:

  1. library(ggplot2)
  2. ggplot(mtcars,aes(qsec,mpg))+
  3. geom_point()+
  4. plt_regression_line(color = &quot;red&quot;,linetype = 1)+
  5. plt_identity_line(color = &quot;blue&quot;,linetype = 1)

如何将两个ggplot2自定义函数合并,使图例分开。

*I use alpha, since col and fill can be used for others variables and I want to show them in the legend.

答案1

得分: 1

你两次使用了'alpha'美学,所以ggplot正在合并图例。一个潜在的选项是为其中一个函数使用另一个美学,例如线宽(linewidth):

  1. library(tidyverse)
  2. plt_regression_line <- function(color = "black", linetype = "dashed"){
  3. list(
  4. ggplot2::geom_smooth(
  5. method = "lm",
  6. se = FALSE,
  7. formula = "y ~ x",
  8. mapping = ggplot2::aes(alpha = "Regression line"),
  9. linetype = linetype,
  10. color = color),
  11. ggplot2::labs(alpha = ""),
  12. ggplot2::scale_alpha_manual(values = c(1,1))
  13. )
  14. }
  15. plt_identity_line <- function(color = "black", linetype = "dashed"){
  16. list(
  17. ggplot2::geom_abline(
  18. mapping = ggplot2::aes(slope = 1, intercept = 0, linewidth = "Identity line"),
  19. linetype = linetype,
  20. color = color),
  21. ggplot2::labs(linewidth = ""),
  22. ggplot2::scale_linewidth_manual(values = c(0.5,0.5))
  23. )
  24. }
  25. ggplot(mtcars,aes(qsec,mpg))+
  26. geom_point()+
  27. plt_regression_line(color = "red",linetype = 1) +
  28. plt_identity_line(color = "blue",linetype = 1)

另一个潜在的选项是使用 ggnewscale 包,例如:

  1. library(tidyverse)
  2. library(ggnewscale)
  3. plt_regression_line <- function(color = "black", linetype = "dashed"){
  4. list(
  5. ggplot2::geom_smooth(
  6. method = "lm",
  7. se = FALSE,
  8. formula = "y ~ x",
  9. mapping = ggplot2::aes(alpha = "Regression line"),
  10. linetype = linetype,
  11. color = color),
  12. ggplot2::labs(alpha = ""),
  13. ggplot2::scale_alpha_manual(values = c(1,1))
  14. )
  15. }
  16. plt_identity_line <- function(color = "black", linetype = "dashed"){
  17. list(
  18. ggplot2::geom_abline(
  19. mapping = ggplot2::aes(slope = 1, intercept = 0, alpha = "Identity line"),
  20. linetype = linetype,
  21. color = color),
  22. ggplot2::labs(alpha = ""),
  23. ggplot2::scale_alpha_manual(values = c(1,1))
  24. )
  25. }
  26. ggplot(mtcars,aes(qsec,mpg))+
  27. geom_point()+
  28. plt_regression_line(color = "red",linetype = 1) +
  29. ggnewscale::new_scale(new_aes = "alpha") +
  30. plt_identity_line(color = "blue",linetype = 1)

这两种解决方案对你的用例有用吗?

英文:

You are using the 'alpha' aesthetic twice, so ggplot is combining the legends. One potential option is to use another aesthetic, e.g. linewidth, for one of the functions:

  1. library(tidyverse)
  2. plt_regression_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  3. list(
  4. ggplot2::geom_smooth(
  5. method = &quot;lm&quot;,
  6. se = FALSE,
  7. formula = &quot;y ~ x&quot;,
  8. mapping = ggplot2::aes(alpha = &quot;Regression line&quot;),
  9. linetype = linetype,
  10. color = color),
  11. ggplot2::labs(alpha = &quot;&quot;),
  12. ggplot2::scale_alpha_manual(values = c(1,1))
  13. )
  14. }
  15. plt_identity_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  16. list(
  17. ggplot2::geom_abline(
  18. mapping = ggplot2::aes(slope = 1, intercept = 0,linewidth = &quot;Identity line&quot;),
  19. linetype = linetype,
  20. color = color),
  21. ggplot2::labs(linewidth = &quot;&quot;),
  22. ggplot2::scale_linewidth_manual(values = c(0.5,0.5))
  23. )
  24. }
  25. ggplot(mtcars,aes(qsec,mpg))+
  26. geom_point()+
  27. plt_regression_line(color = &quot;red&quot;,linetype = 1) +
  28. plt_identity_line(color = &quot;blue&quot;,linetype = 1)

如何将两个ggplot2自定义函数合并,使图例分开。<!-- -->

<sup>Created on 2023-06-19 with reprex v2.0.2</sup>

Another potential option is to use the ggnewscale package, e.g.

  1. library(tidyverse)
  2. library(ggnewscale)
  3. plt_regression_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  4. list(
  5. ggplot2::geom_smooth(
  6. method = &quot;lm&quot;,
  7. se = FALSE,
  8. formula = &quot;y ~ x&quot;,
  9. mapping = ggplot2::aes(alpha = &quot;Regression line&quot;),
  10. linetype = linetype,
  11. color = color),
  12. ggplot2::labs(alpha = &quot;&quot;),
  13. ggplot2::scale_alpha_manual(values = c(1,1))
  14. )
  15. }
  16. plt_identity_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  17. list(
  18. ggplot2::geom_abline(
  19. mapping = ggplot2::aes(slope = 1, intercept = 0,alpha = &quot;Identity line&quot;),
  20. linetype = linetype,
  21. color = color),
  22. ggplot2::labs(alpha = &quot;&quot;),
  23. ggplot2::scale_alpha_manual(values = c(1,1))
  24. )
  25. }
  26. ggplot(mtcars,aes(qsec,mpg))+
  27. geom_point()+
  28. plt_regression_line(color = &quot;red&quot;,linetype = 1) +
  29. ggnewscale::new_scale(new_aes = &quot;alpha&quot;) +
  30. plt_identity_line(color = &quot;blue&quot;,linetype = 1)

如何将两个ggplot2自定义函数合并,使图例分开。<!-- -->

<sup>Created on 2023-06-19 with reprex v2.0.2</sup>

Do either of these solutions work for your use-case?

huangapple
  • 本文由 发表于 2023年6月19日 09:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503084.html
匿名

发表评论

匿名网友

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

确定