使用stat_function在ggplot2中绘制部分阴影的法线 [英] Using stat_function to draw partially shaded normal curve in ggplot2
问题描述
我是R语言的初学者,对于ggplot在创建法线曲线时如何使用变量 x感到非常困惑。
I'm a big beginner in R and am very confused as to how ggplot is using variable "x" when creating normal curves.
我的情况是这样。我正在尝试在给定特定的均值和标准偏差的情况下绘制法线曲线,并且在没有数据的情况下,我看到的最常见的方法如下:
My situation is this. I'm trying to plot normal curves given specific means and standard deviations and in the absence of data the most common way I've seen to do this is as follows:
score = 1800
m = 1500
std = 300
ggplot(data.frame(x = c(300, 2700)), aes(x = x)) + stat_function(fun =
dnorm, args = list(mean = m, sd = std)) + scale_x_continuous(name
= "Score", breaks = seq(300, 2700, std))
我想遮盖曲线的特定区域,因此使用Internet创建了一个函数如下:
I wanted to shade a specific area of the curve so using the Internet I created a function as follows:
funcShaded <- function(x) {
y = dnorm(x, mean = m, sd = std)
y[x < score] <- NA
return(y)
}
用
p + stat_function(fun = funcShaded,geom = area,fill =#84CA72,alpha = .2)在我的曲线上添加了一层
这可以创建我想要的图形。但是,我对此有2个问题。首先,当我将代码分解
This works to create the graph I desire. However, I have 2 questions about this. First, when I break the code down
data.frame(x = c(300, 2700))
创建一个预期的两个项目数据框,因此如何将其用于创建x轴值,以及进一步,要传递给要适当使用的函数(读为好像是值列表)?
creates a two item dataframe as you would expect so how is this capable to being used to create x-axis values and, further, to be passed to the function to be used appropriately (read. as if it were a list of values)?
第二,我现在想重用此函数后来根据不同的分数(例如 score2 = 1630
)填充曲线下的其他区域,并想我可以在 funcShaded中添加另一个变量
传递分数(即 funcShaded<-函数(x,分数))
,然后调用我的 stat_function
如下: p + stat_function(fun = funcShaded(x,score2),...)
但:
Second, I now want to re-use this function later to fill in other area under the curve based on a different score (e.g. score2 = 1630
) and was thinking I could just add another variable to funcShaded
to pass score (i.e. funcShaded <- function(x, score))
and then call my stat_function
as follows: p + stat_function(fun = funcShaded(x, score2), ...)
but:
- 我不确定此语法是否有效
- 似乎是
x
变量永远不会用此代码明确创建,因为它不会显示在我的环境中n我尝试这段代码我得到错误:找不到对象'x'
- I'm not sure this syntax will work
- It seems like the
x
variable is never explicitly "created" with this code because it doesn't show up in my Environment and when I try this code I get Error: object 'x' not found
所以我想我我只是想知道在这种情况下'x'是如何工作的,以及我是否应该根据自己想做的方式来创建它。
So I guess I'm just curious as to how 'x' is working in this situation and if I should be creating it differently given what I want to do.
推荐答案
传递给 stat_function
的函数必须未被调用(除非它返回另一个函数;像 purrr :: partial
之类的副词是这里的另一种方法),因为 stat_function
需要向它传递一个向量的 x
值。
The function passed to stat_function
must be uncalled (unless it returns another function; an adverb like purrr::partial
or the like is another approach here), because stat_function
needs to pass it a vector of x
values.
您已经完成了 dnorm
使用 funcShaded
需要做什么:通过 args
传递其他固定参数:
You've already done with dnorm
what you need to do with funcShaded
: pass additional fixed parameters through args
:
library(ggplot2)
score = 1800
m = 1500
std = 300
funcShaded <- function(x, lower_bound) {
y = dnorm(x, mean = m, sd = std)
y[x < lower_bound] <- NA
return(y)
}
ggplot(data.frame(x = c(300, 2700)), aes(x = x)) +
stat_function(fun = dnorm, args = list(mean = m, sd = std)) +
stat_function(fun = funcShaded, args = list(lower_bound = score),
geom = "area", fill = "#84CA72", alpha = .2) +
scale_x_continuous(name = "Score", breaks = seq(300, 2700, std))
或者,不用编写自己的函数,也可以使用 stat_function
的 xlim $ c做同样的事情$ c>参数:
Alternately, without writing your own function, you can do the same thing with stat_function
's xlim
parameter:
ggplot(data.frame(x = c(300, 2700)), aes(x = x)) +
stat_function(fun = dnorm, args = list(mean = m, sd = std)) +
stat_function(fun = dnorm, args = list(mean = m, sd = std), xlim = c(score, 2700),
geom = "area", fill = "#84CA72", alpha = .2) +
scale_x_continuous(name = "Score", breaks = seq(300, 2700, std))
stat_function
如何使用传递给 x
美学的值,将它们用作在值网格之间进行插值的限制,其数量由其 n
参数设置,默认为101。这与大多数 stats
的用法肯定不同,但这是一个非常有用的功能。
As for how stat_function
uses the values passed into its x
aesthetic, it uses them as limits between which to interpolate a grid of values, the number of which set by its n
parameter, which defaults to 101. It's decidedly a different usage than most stats
, but it's a very useful function.
这篇关于使用stat_function在ggplot2中绘制部分阴影的法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!