首页 \ 问答 \ 使用stat_function在ggplot2中绘制部分阴影的正常曲线(Using stat_function to draw partially shaded normal curve in ggplot2)

使用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_functionp + stat_function(fun = funcShaded(x, score2), ...)但是:

  1. 我不确定这种语法是否有效
  2. 似乎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 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:

  1. I'm not sure this syntax will work
  2. 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

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.


原文:https://stackoverflow.com/questions/48753007
更新时间:2023-03-04 09:03

最满意答案

在许多平台上,Stdout直到新行字符才会刷新。 我认为Windows也是如此。

目前,API没有公开强制fflushstdout

我建议您在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 on stdout.

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注释是元数据的一个示例。 您可以使用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.
  • 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 ...
  • 我很确定你可以通过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. ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)