如何添加图例来指示颜色代码而不是数据?
Posted
技术标签:
【中文标题】如何添加图例来指示颜色代码而不是数据?【英文标题】:How can I add a legend to indicate a color code instead of data? 【发布时间】:2021-12-02 08:32:18 【问题描述】:我正在努力为使用 PwrPlot() 函数创建的绘图添加颜色代码(图例),如下所示。这是一个教学演示,我只需要将标签 (alpha, beta, 1-alpha, 1-beta) 替换为说明每种颜色所指的图例(实际上,更改曲线的参数可能会导致一些'图中的“丑陋”标签位置!)。运行此函数还会发出我无法解释的警告:
警告信息: 1:在 is.na(x) 中: is.na() 应用于“表达式”类型的非(列表或向量)
任何帮助将不胜感激!谢谢
PwrPlot <- function(mu0=0, mu1=1.9, sig0=1, sig1=1, alpha=0.05, tail=1)
ggplot(data.frame(x = -4:5), aes(x)) +
stat_function(fun = dnorm, args = c(mu0, sig0), geom = 'area',
xlim = c(qnorm(1-alpha/tail, mu0, sig0), 5), fill = 'red') +
stat_function(fun = dnorm, args = c(mu0, sig0)) +
stat_function(fun = dnorm, args = c(mu0, sig0), geom = 'area',
xlim = c(-4, qnorm(1-alpha/tail, mu0, sig0)), fill = 'deepskyblue3') +
stat_function(fun = dnorm, args = c(mu0, sig0)) +
stat_function(fun = dnorm, args = c(mu1, sig1)) +
stat_function(fun = dnorm, args = c(mu1, sig1), geom = 'area',
xlim = c(qnorm(1-alpha/tail, mu0, sig0), 5), fill = 'cyan4', alpha=0.2) +
stat_function(fun = dnorm, args = c(mu1, sig1), geom = 'area',
xlim = c(-4, qnorm(1-alpha/tail, mu0, sig0)), fill = 'chocolate3', alpha=0.5) +
geom_text(x=-0.4, y=0.18, label=expression(1-alpha), size=10, col="white") +
geom_text(x=2, y=0.018, label=expression(alpha), size=10, col="white") +
geom_text(x=1, y=0.1, label=expression(beta), size=10, col="white") +
geom_text(x=2.5, y=0.1, label=expression(1-beta), size=10, col="black") +
geom_text(x=-1.7, y=0.35, label="H0", size=10, col="black") +
geom_text(x=3.5, y=0.35, label="H1", size=10, col="black") +
labs(y="Densité")
PwrPlot()
【问题讨论】:
非常感谢!这解决了这个问题,我可以继续前进! 【参考方案1】:你可以把颜色放在aes()
里面,结合scale_fill_identity()
就可以构造一个图例。
library(ggplot2)
PwrPlot <- function(mu0=0, mu1=1.9, sig0=1, sig1=1, alpha=0.05, tail=1)
ggplot(data.frame(x = -4:5), aes(x)) +
stat_function(fun = dnorm, args = c(mu0, sig0), geom = 'area',
xlim = c(qnorm(1-alpha/tail, mu0, sig0), 5),
aes(fill = 'red')) +
stat_function(fun = dnorm, args = c(mu0, sig0)) +
stat_function(fun = dnorm, args = c(mu0, sig0), geom = 'area',
xlim = c(-4, qnorm(1-alpha/tail, mu0, sig0)),
aes(fill = 'deepskyblue3')) +
stat_function(fun = dnorm, args = c(mu0, sig0)) +
stat_function(fun = dnorm, args = c(mu1, sig1)) +
stat_function(fun = dnorm, args = c(mu1, sig1), geom = 'area',
xlim = c(qnorm(1-alpha/tail, mu0, sig0), 5),
aes(fill = 'cyan4'), alpha=0.2) +
stat_function(fun = dnorm, args = c(mu1, sig1), geom = 'area',
xlim = c(-4, qnorm(1-alpha/tail, mu0, sig0)),
aes(fill = 'chocolate3'), alpha=0.5) +
scale_fill_identity(
labels = expression(beta, 1-alpha, 1-beta, alpha),
guide = guide_legend()
) +
annotate(
"text", size = 10,
x = c(-0.4, 2, 1, 2.5, -1.7, 3.5),
y = c(0.18, 0.018, 0.1, 0.1, 0.35, 0.35),
label = expression(1-alpha, alpha, beta, 1-beta, "H0", "H1"),
colour = rep(c("white", "black"), each = 3)
) +
labs(y="Densité")
PwrPlot()
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'
由reprex package (v2.0.1) 于 2021 年 10 月 14 日创建
【讨论】:
以上是关于如何添加图例来指示颜色代码而不是数据?的主要内容,如果未能解决你的问题,请参考以下文章