简体   繁体   English

如何使用 ggplot plot 改变 R 中一个变量的值的方程

[英]How to plot an equation changing value of one variable in R using ggplot

I have an equation that gives values between one and zero and would like to use ggplot in R to plot how changing one of the variables (age) in the equation alters the output value.我有一个方程,它给出的值介于 1 和 0 之间,并且想在 R 到 plot 中使用 ggplot 如何改变方程中的一个变量(年龄)会改变 Z78E6221F6393D1356681DB398CZF1 值。 I'd like the X axis to be a range of the variable from 0 to 100 and to plot the output value of the equation on the X axis.我希望 X 轴是从 0 到 100 的变量范围,并且 plot 是 X 轴上方程的 output 值。

I set the variables passed to the equation like so:我像这样设置传递给方程的变量:

age = 100
d <- (365/4)*age
k <- 5
N <-8
m <- 15000000
Nm <- N*m
p <- 1 - (1 - (1 - (1 - u)^d)^k)^Nm

I am sure there must be a simple way in R to give a range of values for age to pass to the equation and plot the resulting value of P as a line.我确信在 R 中必须有一种简单的方法来给出一系列年龄值以传递给方程,并且 plot 将 P 的结果值作为一条线。 But I am not sure how best to go about it.但我不确定如何最好地了解它。 Do I need to fill in a table of values in the range beforehand or can I put the range itself within the ggplot command?我是否需要事先填写范围内的值表,还是可以将范围本身放在 ggplot 命令中?

Thanks in advance提前致谢

You can use stat_function .您可以使用stat_function

p <- function(age, u, k, N, m)
{
  1 - (1 - (1 - (1 - u)^((365/4) * age))^k)^(N * m)
}

ggplot(data.frame(age = 1:100), aes(x = age)) + 
  stat_function(fun = p, args = list(u = 0.000005, k = 5, N = 8, m = 15000000)) +
  labs(y = "p")

在此处输入图像描述

There was no variable u defined in the question, but this is a logistic curve, and setting u to 0.000005 puts the middle of your range at p = 0.5, so u is presumably near this value.问题中没有定义变量u ,但这是一条逻辑曲线,将u设置为 0.000005 会将范围的中间值置于 p = 0.5,因此u可能接近该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM