2017-03-02 50 views
0

我必須製作4個圖,它們的區別僅在於yylab。 我從data.tabledt這是R:ggplot2並循環data.table

set.seed(123) 
dt <- data.table(a = rnorm(20), 
       b = rnorm(20), 
       c = rnorm(20), 
       d = rnorm(20), 
       e = rnorm(20)) 

每一個情節應該與行號的散點圖作爲x VS y值開始。此外,我想繪製一些hlinemedian(y) + h*mad(y)其中h = c(0, -2, 2, -3, 3)

該地塊應被重複列acddte

我想出了這段代碼

# Defining y labels # 
ylabels <- c(bquote(phantom(.)^100*A~"/"*phantom(.)^200*A), 
      bquote(phantom(.)^101*C~"/"*phantom(.)^201*B), 
      bquote(phantom(.)^102*D~"/"*phantom(.)^202*D), 
      bquote(phantom(.)^103*E~"/"*phantom(.)^202*E)) 

# Selecting columns of dt  
ydata <- names(dt)[c(1, 3, 4, 5)] 


h <- c(0, -2, 2, -3, 3) 
hcol <- c("#009E73", "#E69F00", "#E69F00", "red", "red") 

# The for cycle should create the 4 plots and assign them to a list 
plots <- list() 
for (i in seq_along(ydata)) { 

    p1 <- ggplot(dt, aes_string(x = seq(1, dt[, .N]), y = ydata[i])) + 
        geom_point() + 
        geom_hline(aes_string(yintercept = median(ydata[i]) + 
             h * mad(ydata[i])), color = hcol) + 
        xlab("Replicate") + 
        ylab(ylabels[i]) + 
        scale_x_continuous(breaks = seq(1, dt[,.N]))) 

    plots[[i]] <- p1 # add each plot into plot list 
    } 

然後plots將從Cookbook for R被送入multiplot功能。

但是,我的loop無法正常工作,因爲它無法計算中間值和瘋狂值。 你有任何建議使代碼工作?

回答

0
# data.table with the median +- h* mad values 
hline.values <- dt[, lapply(.SD, function(x) median(x) + h * mad(x)), 
        .SDcols = ydata] 

# new empty list 
plots <- list() 

for (i in seq_along(ydata)) { 

     p1 <- ggplot(dt, aes_string(x = seq(1, dt[, .N]), y = ydata[i])) + 
      geom_point() + 
      geom_hline(data = hline.values, 
         aes_string(yintercept = ydata[i])) + 
      # Axis labels and theme 
      xlab("Replicate") + 
      ylab(ylabels[[i]]) + 
      scale_x_continuous(breaks = seq(1, dt[, .N])) 

     plots[[i]] <- p1 
}