How to find doubling time of cells with scatterplot in R?

前端 未结 3 418
遥遥无期
遥遥无期 2021-01-27 18:45

I\'m trying to calculate the doubling time of cells using a scatterplot. This is my dataframe

df = data.frame(\"x\" = 1:5, \"y\" = c(246, 667, 1715, 4867, 11694)         


        
3条回答
  •  囚心锁ツ
    2021-01-27 19:36

    You can visualize the constant rate of change with ggplot2 by scaling the y-axis accordingly:

    library(dplyr)
    library(ggplot2)
    library(broom)
    library(scales)
    
    df = data.frame("x" = 1:5, "y" = c(246, 667, 1715, 4867, 11694))
    
    fit <- lm(data = df, log2(y) ~ x)
    tidy_fit <- tidy(fit) %>% 
      mutate(x = 3, y = 2048)
    
    ggplot(df, aes(x = x, y = y)) +
      geom_point() +
      scale_y_continuous(name = "log2(y)", 
                         trans = 'log2', 
                         breaks = trans_breaks("log2", function(x) 2^x),
                         labels = trans_format("log2", math_format(2^.x))) +
      geom_smooth(method = "lm", se = FALSE) +
      geom_text(tidy_fit,
                mapping = aes(
                  x = x,
                  y = y,
                  label = paste0("log2(y) = ", round(estimate[1], 2), " + ", round(estimate[2], 2), "x",
                                 "\n", "Doubling Time: ", round(1 / tidy_fit$estimate[2], 2), " Days")
                ),
                nudge_x = -1,
                nudge_y = 0.5,
                hjust = 0)
    

    Created on 2020-02-03 by the reprex package (v0.3.0)

提交回复
热议问题