簡體   English   中英

gem_smooth的ggplot2語法

[英]ggplot2 syntax for geom_smooth

我具有以下函數,用於將非線性曲線擬合到一組數據。 我不斷收到以下錯誤:

Error in do.call("layer", list(mapping = mapping, data = data, stat = stat,  : 
 object 'y' not found 

錯誤來自geom_smooth擬合。 你能提供建議嗎?

下面是一些測試數據和功能:

library("ggplot2") 

DF <- data.frame(RFP_fold=1:20, E=rnorm(20)) 


flimPlot <- function(data) {
    ggplot(data, aes(x=RFP_fold, y=E)) +
    geom_point(shape=1) +
    geom_smooth(method=nls,                     # Add non linear regression fit
            formula='y ~ a * (x / (x + K))',    # Forumla for fit
            start=list(a=max(y), K = 0.1),  # set the parameters
            se = FALSE,         # No shaded CI
            fullrange=TRUE)    # Extend regression line
   }

flimPlot(DF)

# Error in do.call("layer", list(mapping = mapping, data = data, stat = stat,  : 
object 'y' not found 

我不知道為什么變量沒有被傳遞給geom_smooth,所以我只是在函數中添加了一些變量。

flimPlot <- function(data) {

    y <- data$E
    x <- data$RFP_fold
    a <- max(dat$E)
    st <- list(a=a, K=0.1)
    fit <- nls(formula="E ~ a * (RFP_fold / (RFP_fold + K))", dat, start=st )
    K <- round(coef(fit))

    ggplot(data, aes(x=RFP_fold, y=E)) +
    geom_point(shape=1) +
    #geom_text(aes(3,15, label = paste(expr, K, sep=""))) +
    #annotate("text", x = 3, y = 15, label = paste(expr, K, sep="") ) +
    stat_smooth( geom = "smooth",
                 method=nls,   # Add non linear regression fit
                 formula="y ~ a * (x / (x + K))",    # Forumla for fit
                 start=st,  # set the parameters
                 #se = FALSE,         # No shaded CI
                 fullrange=TRUE) +   # Extend regression line
    scale_y_continuous(limits = c(0, 20)) +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(), 
          panel.background = element_blank(), 
          axis.line = element_line(color = 'black'),
          text = element_text(size=12),
          #axis.title.x = element_blank(),
          #axis.title.y = element_blank(),
          legend.justification=c(1,0)) +
    ylab("FRET E%") +
    xlab("acceptor intensity")
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM