以下是我现在的代码:
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
)
这是输出:
根据我从 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
我已经进行了大量搜索,但无法解决此问题。
答案 0 :(得分:5)
您在第三次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
)
如果您在第三个color=NA
中设置stat_function()
,则会得到此信息(可能稍微更合适):</ p>