【问题标题】:How do I reproduce a plot in ggplot based on nonlinear regressions from the R package Growthrates?如何根据 R 包 Growthrates 的非线性回归在 ggplot 中重现绘图?
【发布时间】:2020-01-27 12:41:38
【问题描述】:

我正在使用 Growthrates 包为我的数据生成增长率曲线的参数估计值。我已经进行了回归并查看了生成的图,我对数据感到满意,但我想在 ggplot2 中重现以下图。

图一:Multiplot of a regression for each group:treatment combo

我想要每个组的回归线的多图:治疗组合,但我对其执行的所有回归((即逻辑、gompertz、gompertz2 等)。到目前为止,我有:

library(growthrates)

####Using logistic regression to fit the data across mutliple groups
p     <- c(y0 = 1, mumax = 0.5, K = 200)
lower <- c(y0 = 0, mumax = 0,   K = 20)
upper <- c(y0 = 100, mumax = 5,   K = 400)

many_logistics <- all_growthmodels(y_data ~ 
                                     grow_logistic(total_time_days, parms) | sample + treatment,
                                   data = Alldata, 
                                   p = p,
                                   lower = lower, 
                                   upper = upper, 
                                   log = "y")
pp   <- coef(many_logistics)

par(mfrow = c(5, 3))
par(mar = c(2.5, 4, 2, 1))
plot(many_logistics)

many_logistics_results <- results(many_logistics)
xyplot(mumax ~ treatment | sample, data = many_logistics_results, layout = c(3, 1))
xyplot(r2 ~ treatment | sample, data = many_logistics_results, layout = c(3, 1))
xyplot(K ~ treatment | sample, data = many_logistics_results, layout = c(3, 1))

curve_logistics <- predict(many_logistics) #Prediction for given data (data for curve)
est_logistics <- predict(many_logistics, newdata=data.frame(time=seq(0, 1, 0.1))) #Extrapolation/Interpolation from curve


####Using Gompertz regression to fit the data across mutliple groups
p     <- c(y0 = 1, mumax = 0.5, K = 200)
lower <- c(y0 = 0, mumax = 0,   K = 20)
upper <- c(y0 = 100, mumax = 5,   K = 400)

many_gompertz <- all_growthmodels(y_datay_data ~ 
                                    grow_gompertz(total_time_days, parms) | sample + treatment,
                                   data = Alldata, 
                                   p = p,
                                   lower = lower, 
                                   upper = upper)
pp   <- coef(many_gompertz)

par(mfrow = c(5, 3))
par(mar = c(2.5, 4, 2, 1))
plot(many_gompertz)

many_gompertz_results <- results(many_gompertz)
xyplot(mumax ~ treatment | sample, data = many_gompertz_results, layout = c(3, 1))
xyplot(r2 ~ treatment | sample, data = many_gompertz_results, layout = c(3, 1))
xyplot(K ~ treatment | sample, data = many_gompertz_results, layout = c(3, 1))

curve_gompertz <- predict(many_gompertz) #Prediction for given data (data for curve)
est_gompertz <- predict(many_gompertz, newdata=data.frame(time=seq(0, 1, 0.1))) #Extrapolation/Interpolation from curve

#Prepare the data frames

curve_logistics2 <- curve_logistics %>% 
  map_df(as_tibble, .id = "src") %>%
  separate(src, c("sample", "treatment"), ":") %>%
  mutate(regression = "logistic")

curve_gompertz2 <- curve_gompertz %>% 
  map_df(as_tibble, .id = "src") %>%
  separate(src, c("sample", "treatment"), ":") %>%
  mutate(regression = "gompertz")

alldata2<- Alldata %>%
  select("sample", "treatment","total_time_days", "y_data") %>%
  rename(time = "total_time_days") %>%
  rename(y = "y_data") %>%
  mutate(regression = "none") 

comp_reg <- bind_rows(curve_logistics2, curve_gompertz2, alldata2)

#define the function to automatically generate plots#define the function to automatically generate plots

REGRESSION_LINE_PLOT <-function(x) {ggplot(data = x, aes(x=time, y=y, colour = regression, linetype = regression)) + 
    geom_point(size = 2.5, data = subset(x, regression %in% c("none"))) +
    stat_smooth(data = subset(x, regression %in% c("gompertz", "logistic"))) +
    theme_bw() + 
    theme(panel.grid.major=element_blank(),
          panel.grid.minor=element_blank(),
          panel.spacing = unit(0,"cm"),
          axis.line=element_line(colour="black"),
          # axis.title.x = element_text(size=14, colour = "black"),
          axis.title.x = element_blank(),
          # axis.title.y = element_text(size=14, colour = "black"),
          axis.title.y = element_blank(),
          # axis.text.y = element_text(size=14, colour = "black"),
          # axis.text.x = element_text(size=14, colour = "black"),
          strip.background = element_blank(),
          strip.text = element_text(size = 12, colour="black", face = "bold"),
          legend.text= element_text(size = 12, colour = "black"),
          legend.title=element_blank(), 
          text = element_text(size=12,  family="Arial")) +
    # plot.margin=unit(c(0.1,0.1,0.1,0.1),"cm")) +
    #scale_colour_manual(values = cbbPalette) + ### here I tell R to use my custom colour palette
    #scale_x_continuous(limits = c(-1,14)) + # set time range from -1 to 70 since we started sampling on day -1
    #scale_y_continuous(limits = c(-1,350), breaks = seq(0, 360, 90)) + # For comparison purposes, i want all my panels to have the same y axis scale
    ylab("") + 
    xlab("")
}

comp_reg_nested<- comp_reg %>%
  group_by(sample, treatment) %>%
  nest() %>%
  mutate(plots=map(.x=data, ~REGRESSION_LINE_PLOT(.x)))

fo_ad_line <- comp_reg_nested[[1,"plots"]]

但是,我认为回归线在 ggplot22 中没有正确表示。有没有更好的方法来做到这一点?

【问题讨论】:

  • 您好,您的代码遗漏了一个可重现的数据示例。

标签: r ggplot2 non-linear-regression


【解决方案1】:

我从包的内置数据创建了一个或多或少类似于您的数据结构的数据示例,并稍微简化了代码,省略了默认的绘图函数。非常喜欢你map_df的数据框构造方法,谢谢。然后我添加了一个简单的 ggplot,它当然可以根据您的需要进行扩展和调整。

library(growthrates)
library(dplyr)
library(purrr)
library(tidyr)
library(ggplot2)

## use subset of built-in example data of the package
## and adapt it to the existing part of the script
data(bactgrowth)
Alldata <- bactgrowth[(bactgrowth$conc < 1) & bactgrowth$replicate == 1, ]
names(Alldata)    <- c("sample", "replicate", "treatment", "total_time_days", "y_data")
Alldata$y_data    <- Alldata$y_data * 1000
Alldata$treatment <- as.character(Alldata$treatment)

####Using logistic regression to fit the data across mutliple groups
p     <- c(y0 = 1, mumax = 0.5, K = 200)
lower <- c(y0 = 0, mumax = 0,   K = 20)
upper <- c(y0 = 100, mumax = 5,   K = 400)

many_logistics <- all_growthmodels(y_data ~
  grow_logistic(total_time_days, parms) | sample + treatment,
  data = Alldata,
  p = p,
  lower = lower,
  upper = upper)

many_logistics_results <- results(many_logistics)
curve_logistics <- predict(many_logistics)

####Using Gompertz regression to fit the data across mutliple groups
many_gompertz <- all_growthmodels(y_data ~
  grow_gompertz(total_time_days, parms) | sample + treatment,
  data = Alldata,
  p = p,
  lower = lower,
  upper = upper)

many_gompertz_results <- results(many_gompertz)
curve_gompertz <- predict(many_gompertz)

#Prepare the data frames
curve_logistics2 <- curve_logistics %>%
  map_df(as_tibble, .id = "src") %>%
  separate(src, c("sample", "treatment"), ":") %>%
  mutate(regression = "logistic")

curve_gompertz2 <- curve_gompertz %>%
  map_df(as_tibble, .id = "src") %>%
  separate(src, c("sample", "treatment"), ":") %>%
  mutate(regression = "gompertz")

alldata2<- Alldata %>%
  rename(time = "total_time_days", y = "y_data")

## combine the two curves to a joint data frame
comp_reg <- bind_rows(curve_logistics2, curve_gompertz2)

## plot it
ggplot(comp_reg, aes(time, y)) +
  geom_point(data = alldata2) +
  geom_line(aes(color = regression)) +
  facet_grid(treatment ~ sample)

【讨论】:

  • 感谢您提供示例!我唯一剩下的问题是,当您在 ggplot 中使用 geom_line 时,它​​只会在预测函数的点之间绘制线。这不是一条平滑的曲线,所以我想知道 stat_smooth 是否更合适并更好地代表实际的建模曲线?还是使用 geom_smooth() 但迭代所有处理和样本?
  • 我想通了。我只有几个实际时间点(不幸的是),而 predict() 仅预测了我拥有的那些真实点的点。所以我将 curve_gompertz
  • 是的,完全正确。最简单的方法就是使用 predict 的“newdata”参数。作为替代方案,您也可以直接使用增长函数及其参数。
猜你喜欢
  • 2012-01-12
  • 2016-07-09
  • 2016-09-04
  • 2017-03-17
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
相关资源
最近更新 更多