英文:
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)
*我使用了 alpha
,因为 col
和 fill
可以用于其他变量,并且我希望在图例中显示它们。
英文:
I have two custom functions:
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))
)
}
and
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))
)
}
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 = "red",linetype = 1)+
plt_identity_line(color = "blue",linetype = 1)
*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 <- 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)
<!-- -->
<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 <- 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)
<!-- -->
<sup>Created on 2023-06-19 with reprex v2.0.2</sup>
Do either of these solutions work for your use-case?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论