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

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

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

问题

我有两个自定义函数:

plt_regression_line <- function(color = "black", linetype = "dashed"){
  list(
    ggplot2::geom_smooth(
      method = "lm",
      se = FALSE,
      formula = "y ~ x",
      mapping = ggplot2::aes(alpha = "回归线"),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = ""),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}


plt_identity_line <- function(color = "black", linetype = "dashed"){
  list(
    ggplot2::geom_abline(
      mapping = ggplot2::aes(slope = 1, intercept = 0, alpha = "一致性线"),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = ""),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

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

library(ggplot2)

ggplot(mtcars,aes(qsec,mpg))+
  geom_point()+
  plt_regression_line(color = "red", linetype = 1)+
  plt_identity_line(color = "blue", linetype = 1)

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

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

英文:

I have two custom functions:

plt_regression_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  list(
    ggplot2::geom_smooth(
      method = &quot;lm&quot;,
      se = FALSE,
      formula = &quot;y ~ x&quot;,
      mapping = ggplot2::aes(alpha = &quot;Regression line&quot;),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = &quot;&quot;),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

and

plt_identity_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  list(
    ggplot2::geom_abline(
      mapping = ggplot2::aes(slope = 1, intercept = 0,alpha = &quot;Identity line&quot;),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = &quot;&quot;),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

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

library(ggplot2)

  ggplot(mtcars,aes(qsec,mpg))+
  geom_point()+
  plt_regression_line(color = &quot;red&quot;,linetype = 1)+
  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):

library(tidyverse)

plt_regression_line <- function(color = "black", linetype = "dashed"){
  list(
    ggplot2::geom_smooth(
      method = "lm",
      se = FALSE,
      formula = "y ~ x",
      mapping = ggplot2::aes(alpha = "Regression line"),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = ""),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

plt_identity_line <- function(color = "black", linetype = "dashed"){
  list(
    ggplot2::geom_abline(
      mapping = ggplot2::aes(slope = 1, intercept = 0, linewidth = "Identity line"),
      linetype = linetype,
      color = color),
    ggplot2::labs(linewidth = ""),
    ggplot2::scale_linewidth_manual(values = c(0.5,0.5))
  )
}

ggplot(mtcars,aes(qsec,mpg))+
  geom_point()+
  plt_regression_line(color = "red",linetype = 1) +
  plt_identity_line(color = "blue",linetype = 1)

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

library(tidyverse)
library(ggnewscale)

plt_regression_line <- function(color = "black", linetype = "dashed"){
  list(
    ggplot2::geom_smooth(
      method = "lm",
      se = FALSE,
      formula = "y ~ x",
      mapping = ggplot2::aes(alpha = "Regression line"),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = ""),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

plt_identity_line <- function(color = "black", linetype = "dashed"){
  list(
    ggplot2::geom_abline(
      mapping = ggplot2::aes(slope = 1, intercept = 0, alpha = "Identity line"),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = ""),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

ggplot(mtcars,aes(qsec,mpg))+
  geom_point()+
  plt_regression_line(color = "red",linetype = 1) +
  ggnewscale::new_scale(new_aes = "alpha") +  
  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:

library(tidyverse)

plt_regression_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  list(
    ggplot2::geom_smooth(
      method = &quot;lm&quot;,
      se = FALSE,
      formula = &quot;y ~ x&quot;,
      mapping = ggplot2::aes(alpha = &quot;Regression line&quot;),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = &quot;&quot;),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

plt_identity_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  list(
    ggplot2::geom_abline(
      mapping = ggplot2::aes(slope = 1, intercept = 0,linewidth = &quot;Identity line&quot;),
      linetype = linetype,
      color = color),
    ggplot2::labs(linewidth = &quot;&quot;),
    ggplot2::scale_linewidth_manual(values = c(0.5,0.5))
  )
}

ggplot(mtcars,aes(qsec,mpg))+
  geom_point()+
  plt_regression_line(color = &quot;red&quot;,linetype = 1) +
  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.

library(tidyverse)
library(ggnewscale)

plt_regression_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  list(
    ggplot2::geom_smooth(
      method = &quot;lm&quot;,
      se = FALSE,
      formula = &quot;y ~ x&quot;,
      mapping = ggplot2::aes(alpha = &quot;Regression line&quot;),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = &quot;&quot;),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

plt_identity_line &lt;- function(color = &quot;black&quot;, linetype = &quot;dashed&quot;){
  list(
    ggplot2::geom_abline(
      mapping = ggplot2::aes(slope = 1, intercept = 0,alpha = &quot;Identity line&quot;),
      linetype = linetype,
      color = color),
    ggplot2::labs(alpha = &quot;&quot;),
    ggplot2::scale_alpha_manual(values = c(1,1))
  )
}

ggplot(mtcars,aes(qsec,mpg))+
  geom_point()+
  plt_regression_line(color = &quot;red&quot;,linetype = 1) +
  ggnewscale::new_scale(new_aes = &quot;alpha&quot;) +  
  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:

确定