试图在某个地方回答this question我用 ggplot()geom_smooth() 和我认为环境打了一堵墙 .

我用广泛可用的数据集和基本函数做了一个简洁的问题示例,希望它可以扩展到实际数据集的真正问题 . 模型规范只是为了制作一个代表,没有任何意义 .

我想为5个子集数据绘制5条拟合曲线 . 所有5个模型使用相同的公式,但启动参数特定于每个子集,一个变量的 min() 和其他变量的 max() . geom_smooth()aes(..., color = variable_thar_subsets )应该相对容易 . 如果我事先设定参数,那就是这样 .

1.工作,但这不是我想要的 .

library(tidyverse)
msleep %>% 
ggplot(aes(x = sleep_rem, y = sleep_total, color = vore)) +
  geom_point() +
  geom_smooth(method = "nls", 
              se = FALSE, 
              formula = y ~ max_sleep_total * x / (min_sleep_rem + x),
              method.args = list(start = list(max_sleep_total = 19.9, 
                                              min_sleep_rem   = 1.88)))
#Produces warnings becasuse of missing values. Not the issue, I think.

我一直在尝试制作 ggplot() -or stat_smooth() 的不同方法,我真的不知道谁在这里做主,这可能是问题 - 计算执行时间的起始参数,每个子集一个 . 我的第一次尝试是使用 aes(...) 变量的"internal"名称,如 aes(...) 中所定义 .

2. aes()中的变量名称无效 .

msleep %>% 
  ggplot(aes(x = sleep_rem, y = sleep_total, color = vore)) +
  geom_point() +
  geom_smooth(method = "nls", 
              se = FALSE, 
              formula = y ~ max_sleep_total * x / (min_sleep_rem + x),     #Here y = sleep_total
              method.args = list(start = list(max_sleep_total = max(y),    #Here isn't. 
                                              min_sleep_rem   = min(x))))
# Returns:

#Error in geom_smooth(method = "nls", se = FALSE, formula = y ~ max_sleep_total *  : 
#  objeto 'y' no encontrado
# object y not found

所以,如果我在 data.frame 中调用列名,它可能会起作用 .

3. data =中的列名称,无法正常工作 .

msleep %>% 
  ggplot(aes(x = sleep_rem, y = sleep_total, color = vore)) +
  geom_point() +
  geom_smooth(method = "nlsLM", 
              se = FALSE, 
              formula = y ~ max_sleep_total * x / (min_sleep_rem + x),
              method.args = list(start = list(max_sleep_total = max(sleep_total), 
                                              min_sleep_rem   = min(sleep_rem))))
#Returns:
#Error in geom_smooth(method = "nlsLM", se = FALSE, formula = y ~ max_sleep_total *  : 
#  objeto 'sleep_total' no encontrado

4.全球环境中的变量,工作但没用 .

max_sleep_total <-c(0, 19.9)  # Assign in global environment
min_sleep_rem <- c(1.88, 10) #Two values to check that min and max works.

msleep %>% 
  ggplot(aes(x = sleep_rem, y = sleep_total, color = vore)) +
  geom_point() +
  geom_smooth(method = "nls", 
              se = FALSE, 
              formula = y ~ max_sleep_total * x / (min_sleep_rem + x),
              method.args = list(start = list(max_sleep_total = max(max_sleep_total), 
                                              min_sleep_rem   = min(min_sleep_rem)))) +
  labs(title = "Code block 4")

返回此图,与1相同 .

enter image description here

再一次,不是我想要的 . start = 参数是固定的,我想为每个数据子集提供一组特定的参数 .

问题 .

  • 有没有办法制作我想要的情节 using geom_smooth() 我还没试过?

我知道我可以提前适应每个模型, predict() ,并使用 y 的每个估计值绘制 geom_line() . 它's cumbersome, because you have to create extra data points to get a smooth line, you have to data sets (points and lines), labeling becomes a manual affair and doen'回答第二个问题:

  • 有没有办法让 geom_smooth()method.args() 中查找父框架(*)中的变量,就像对公式一样 . 即使第二个代码有效 . 这应该是显而易见的解决方案 .

(*)我假设是 ggplot() 函数调用,但NSE不太确定 .

背景 .

这两个很好并且帮了很多,但是没有解决subseting的问题也没有使用 color = 参数 .

Fitting with ggplot2, geom_smooth and nls

how to use method="nlsLM" (in packages minpack.lm) in geom_smooth

关于 ggplot() 和环境的有趣咆哮,但针对不同的问题 .

Use of ggplot() within another function in R

试图将环境=环境()扔到墙上,看看它是否明智地将我的思想包裹在环境和词汇范围内寻找解决方案,但无济于事 .