[英]How can I add a curve to a scatter plot if ggplot2?
I'm conducting an audit of paracetamol overdoses. 我正在对扑热息痛服用过量进行审核。 I'm creating a scatter plot of blood levels vs time post ingestion.
我正在创建一个摄食后血液水平与时间的散点图。 This is pretty simple: however, treatment is governed by whether their blood level is above or below the line
y=1986*0.840897^t
where t
is time and y
= the blood level for the values t=4:16
. 这非常简单:但是,治疗取决于他们的血液水平是高于还是低于线
y=1986*0.840897^t
,其中t
是时间, y
=值t=4:16
y=1986*0.840897^t
的血液水平。
How can I superimpose a function like that on top of a scatter plot with ggplot2
? 如何使用
ggplot2
在散点图的顶部叠加类似的函数?
Essentially I want to create something a bit like this: 本质上,我想创建如下所示的内容:
Define your own function, pass it to stat_function()
and you're all set. 定义自己的函数,将其传递给
stat_function()
,就一切stat_function()
。
set.seed(357)
# simulate some data, rnorm() is used to add some noise
x <- seq(from = 4, to = 6, length.out = 10)
y <- 1986*0.840897^x + rnorm(10, sd = 30)
xy <- data.frame(x, y)
# define function
myfun <- function(x) 1986*0.840897^x
library(ggplot2)
ggplot(xy, aes(x = x, y = y)) +
theme_bw() +
geom_point() +
stat_function(fun = myfun)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.