使用stat_function在ggplot2中绘制部分阴影的正常曲线(Using stat_function to draw partially shaded normal curve in ggplot2)
我在R中是一个很大的初学者,我很困惑ggplot在创建普通曲线时如何使用变量“x”。
我的情况是这样的。 我试图在给定特定方法和标准偏差的情况下绘制正态曲线,并且在没有数据的情况下,我看到的最常见的方法如下:
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我创建了一个函数如下:
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)
向我的曲线添加一个图层p + stat_function(fun = funcShaded, geom="area", fill="#84CA72", alpha=.2)
这可以创建我想要的图形。 但是,我有两个问题。 首先,当我打破代码时
data.frame(x = c(300, 2700))
如您所期望的那样创建一个两项数据框,这样如何能够用于创建x轴值,并进一步传递给要正确使用的函数(读取。就好像它是一个值列表)?
其次,我现在想要稍后重新使用这个函数来根据不同的分数(例如
score2 = 1630
)填充曲线下的其他区域,并且认为我可以将另一个变量添加到funcShaded
以通过分数(即funcShaded <- function(x, score))
然后按如下方式调用我的stat_function
:p + stat_function(fun = funcShaded(x, score2), ...)
但是:
- 我不确定这种语法是否有效
- 似乎
x
变量从未使用此代码显式“创建”,因为它不会出现在我的环境中,当我尝试此代码时,我得到错误:找不到对象'x'所以我想我只是好奇'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))
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) }
And then added a layer to my curve with
p + stat_function(fun = funcShaded, geom="area", fill="#84CA72", alpha=.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))
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)?
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 tofuncShaded
to pass score (i.e.funcShaded <- function(x, score))
and then call mystat_function
as follows:p + stat_function(fun = funcShaded(x, score2), ...)
but:
- 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 foundSo 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.
原文:https://stackoverflow.com/questions/48753007
最满意答案
在许多平台上,Stdout直到新行字符才会刷新。 我认为Windows也是如此。
目前,API没有公开强制
fflush
在stdout
。我建议您在http://dartbug.com/new上提交功能请求。
Stdout does not flush until a new-line character, on many platforms. I think this is true for Windows as well.
Currently the API does not expose a way to force a
fflush
onstdout
.I would recommend that you file a feature request at http://dartbug.com/new.
相关问答
更多-
这目前不可能,但我用一个包装来解决它: var extension = require('./dartvsjs/extension.js'); exports.activate = extension.__lib__extension.activate; exports.deactivate = extension.__lib__extension.deactivate; This isn't currently possible but I worked around it with a wrappe ...
-
@override的Dart代码(@override of Dart code)[2023-05-22]
@override注释是元数据的一个示例。 您可以使用Mirrors在代码中检查这些内容。 这是一个简单的示例,它检查子类中的m1()方法是否具有@verride注释: import 'package:meta/meta.dart'; import 'dart:mirrors'; class A { m1() {} } class B extends A { @override m1() {} } void main() { ClassMirror classMirror = reflec ... -
我也有同样的问题。 看起来这是analyzer包0.27.0及更高版本的问题。 我通过将pubspec.yaml的版本pubspec.yaml为<0.27.0来解决它。 I have the same problem. Looks like it is an issue with the analyzer package 0.27.0 and later. I solved it by pinning the version in pubspec.yaml to <0.27.0.
-
使用dart下载文件(Using dart to download a file)[2021-08-24]
Shailen的回应是正确的,甚至可以通过Stream.pipe稍微缩短。 import 'dart:io'; main() { new HttpClient().getUrl(Uri.parse('http://example.com')) .then((HttpClientRequest request) => request.close()) .then((HttpClientResponse response) => response.pipe(new Fil ... -
dart:html库在客户端使用,可以编译为JavaScript。 使用dart:io作为服务器端代码。 在典型的设置中,您有一个Web服务器从客户端侦听WebSocket连接,在这种情况下,您将使用来自dart:html 。 但是,如果您需要,当然您也可以在服务器上启动WebSocket连接:) The dart:html library is used on the client-side and can be compiled to JavaScript. Use dart:io for server ...
-
您可能正在导入自己的库,如下所示: import “package:mylib/lib.dart”; 如果是这样,您应该将断点放在项目中“packages”文件夹下的文件中。 或者,您可以使用相对路径导入lib: import “../lib/lib.dart”; You are probably importing your own library like this: import “package:mylib/lib.dart”; If so, you should put your break ...
-
在许多平台上,Stdout直到新行字符才会刷新。 我认为Windows也是如此。 目前,API没有公开强制fflush在stdout 。 我建议您在http://dartbug.com/new上提交功能请求。 Stdout does not flush until a new-line character, on many platforms. I think this is true for Windows as well. Currently the API does not expose a way ...
-
显然,使用DART_FLAGS也适用于Windows。 请参阅https://www.dartlang.org/tools/dartium/#using-command-line-flags 命令标志以/为前缀,因此您使用: C:\path\to\dartium\chrome.exe /DART_FLAGS='--checked' Apparently, using DART_FLAGS works as well for Windows. see https://www.dartlang.org/to ...
-
Dart导入包无效(Dart import packages not working)[2022-12-10]
我很确定你可以通过pub cache repair (在命令行上执行)或者删除pub cache目录来解决这个问题。 I'm pretty sure you can solve this with pub cache repair (execute on the command line) or alternatively delete the pub cache directory. -
您可以使用与Darteditor中集成的Web服务器类似的pub serve 。 如果您使用Dartium作为浏览器,您可以添加--no-dart2js因此pub serve --no-dart2js生成JavaScript(这很慢)但直接提供Dartium可以直接处理的Dart。 对于其他浏览器,使用集成的dart2js转换当然很方便。 You can use pub serve which is similar to the webserver integrated in the Darteditor. ...