0
我必須製作4個圖,它們的區別僅在於y
和ylab
。 我從data.table
dt
這是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
值開始。此外,我想繪製一些hline
在median(y) + h*mad(y)
其中h = c(0, -2, 2, -3, 3)
該地塊應被重複列a
,c
,d
和dt
e
。
我想出了這段代碼
# 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
無法正常工作,因爲它無法計算中間值和瘋狂值。 你有任何建議使代碼工作?