gpt4 book ai didi

r - 使用 stat_function 和 ggplot 为区域着色

转载 作者:行者123 更新时间:2023-12-04 12:27:09 26 4
gpt4 key购买 nike

这是我现在拥有的代码:

library(ggplot2)

normal <- function(mu, sigma, x){
1/(sigma*sqrt(2*pi))*exp(-((x-mu)/sigma)^2)
}

normal_expr <- function(){
expression(N~bgroup('(',paste(x, '; ',mu, ',', sigma),')') == frac(1, sigma~sqrt(2*pi)) ~
exp~bgroup('[',-~bgroup('(',frac(x-mu,sigma),')')^2,']'))

}


ggplot(data.frame(x=c(-3,3)), aes(x=x, color=g)) +
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(1)), fun=normal, geom='line',
args=list(mu=0.5, sigma=2)) +
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal, geom='line',
args=list(mu=1, sigma=2)) +
scale_x_continuous(breaks=seq(from=-2, to = 3, by=1)) +
ylab(normal_expr()) +
coord_cartesian(ylim=c(0, 0.2)) +
scale_color_manual('',values=c('blue','red', 'red'),
labels=c(expression(N(mu == 0.5, sigma==2)),expression(N(mu == 1, sigma==2)))) +
theme(panel.background = element_rect(fill='white'),
#panel.background has a gray-like color by default
panel.border=element_rect(fill=NA),
#panel.border puts in fill by default
legend.background = element_blank(),
legend.box = 'vertical',
legend.position=c(0.85,0.85),
legend.text.align=0
)

这是输出:

enter image description here

从我从 Chang 的 R Graphics Cookbook 收集的信息来看,我应该能够添加类似的内容
normal_shade <- function(mu, sigma, x){
y <- normal(mu=mu, sigma=sigma, x)
y[x < 0 | x > 2] <- NA
return(y)
}

+ stat_function(fun=normal_shade, geom = 'area', fill = 'red', alpha = 0.2, args =
list(mu = 1, sigma = 2))

到上面的代码,在上面的红线下从 x = 0 到 x = 2 获得阴影。

这是发生的事情:
library(ggplot2)

normal <- function(mu, sigma, x){
1/(sigma*sqrt(2*pi))*exp(-((x-mu)/sigma)^2)
}

normal_expr <- function(){
expression(N~bgroup('(',paste(x, '; ',mu, ',', sigma),')') == frac(1, sigma~sqrt(2*pi))
~ exp~bgroup('[',-~bgroup('(',frac(x-mu,sigma),')')^2,']'))

}

normal_shade <- function(mu, sigma, x){
y <- normal(mu=mu, sigma=sigma, x)
y[x < 0 | x > 2] <- NA
return(y)
}


ggplot(data.frame(x=c(-3,3)), aes(x=x, color=g)) +
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(1)), fun=normal, geom='line',
args=list(mu=0.5, sigma=2)) +
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal, geom='line',
args=list(mu=1, sigma=2)) +
stat_function(fun=normal_shade, geom = 'area', fill = 'red', alpha = 0.2,
args=list(mu=1, sigma=2)) +
scale_x_continuous(breaks=seq(from=-2, to = 3, by=1)) +
ylab(normal_expr()) +
coord_cartesian(ylim=c(0, 0.2)) +
scale_color_manual('',values=c('blue','red', 'red'),
labels=c(expression(N(mu == 0.5, sigma==2)),expression(N(mu == 1, sigma==2)))) +
theme(panel.background = element_rect(fill='white'),
#panel.background has a gray-like color by default
panel.border=element_rect(fill=NA),
#panel.border puts in fill by default
legend.background = element_blank(),
legend.box = 'vertical',
legend.position=c(0.85,0.85),
legend.text.align=0
)

Error in eval(expr, envir, enclos) : object 'g' not found

我已经做了很多搜索,但一直无法解决这个问题。

最佳答案

您遗漏了第三个 stat_function() 的数据称呼。当你把它放回去时,它看起来像这样:

ggplot(data.frame(x=c(-3,3)), aes(x=x, color=g)) + 
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(1)), fun=normal, geom='line',
args=list(mu=0.5, sigma=2)) +
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal, geom='line',
args=list(mu=1, sigma=2)) +
stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal_shade, geom = 'area', fill = 'red', alpha = 0.2,
args=list(mu=1, sigma=2)) +
scale_x_continuous(breaks=seq(from=-2, to = 3, by=1)) +
ylab(normal_expr()) +
coord_cartesian(ylim=c(0, 0.2)) +
scale_color_manual('',values=c('blue','red', 'red'),
labels=c(expression(N(mu == 0.5, sigma==2)),expression(N(mu == 1, sigma==2)))) +
theme(panel.background = element_rect(fill='white'),
#panel.background has a gray-like color by default
panel.border=element_rect(fill=NA),
#panel.border puts in fill by default
legend.background = element_blank(),
legend.box = 'vertical',
legend.position=c(0.85,0.85),
legend.text.align=0
)

enter image description here

如果你设置 color=NA在第三 stat_function()调用你,你会得到这个(这可能稍微更可取):

enter image description here

关于r - 使用 stat_function 和 ggplot 为区域着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com