在ggplot2中绘制x和y的函数

问题描述 投票:0回答:1

我正在尝试使用 ggplot2 在 R 中重新创建该图。本质上,我遇到的问题是我不知道如何绘制与 TAS 变量相对应的线。我知道

TAS = sqrt(x*y/978)
的函数,但我无法在 stat_function 中传递带有 x 和 y 参数的函数。有谁知道如何实现这一目标?

非常感谢您的宝贵时间。

我的代码:

ggplot(df, aes(x = cc_q75,y = ss_ngene)) + 
  geom_point() + 
  geom_function(fun = function(x,y) sqrt(x*y/978)) + 
  geom_hline(yintercept = 200) + 
  geom_vline(xintercept = 0.2)+
  theme_minimal()

编辑:添加了我的输入示例

head(df, n = 20)

cc_q75 ss_ngene
 <num>    <int>
0.27      167
0.31      158
0.57      310
0.48      172
0.63      220
0.21      135
0.28      136
0.48      276
0.59      319
0.42      251
0.34      162
0.27       95
0.71      429
0.29      104
0.57      409
0.72      537
0.57      429
0.59      400
0.40      235
0.37      146
r function ggplot2
1个回答
0
投票

我认为

stat_fun
是当 y 是 x 的函数时专门制作的。相反,使用数据框:

TAS = 0.8

TASdf <- data.frame(x = seq(0.01, 1, by = 0.01))
TASdf <- cbind(TASdf, y = 0.8 ^ 2 * 978 / TASdf$x)

ggplot(df, aes(x = cc_q75,y = ss_ngene)) + 
  geom_point() + 
  geom_line(data = TASdf, aes(x = TASdf$x, y = TASdf$y)) + 
  geom_hline(yintercept = 200) + 
  geom_vline(xintercept = 0.2)+
  theme_minimal() +
  ylim(c(0, 1000))
© www.soinside.com 2019 - 2024. All rights reserved.