I've tried this
fdist <- function(X) { for(i in 1:ncol(X)){ print(i) gg <- ggplot(X, aes(x=X[,i])) + stat_ecdf(geom = "step") gg <- gg + labs(x=paste(names(X[,i]))) assign(paste(names(X[,i]), i, sep = ''), gg + labs(x = names(X[,i]))) end} }
but at first I got all plots saved with the numeration as names, and now isn't even getting to save the plots as R elements.
Thanks for help.
First, use a data frame. ggplot2
works much better with data frames.
df = as.data.frame(X)
Better to create your plots in a named list than as many separate objects. The creation can be simple using lapply
like this:
plot_list = lapply(names(df), function(x) {
ggplot(df[x], aes_string(x = x)) +
stat_ecdf(geom = "step")
})
Looping/ lapply
ing over the names means the default label will be correct. And use aes_string()
instead of aes()
when using a column name stored in a variable.
If you arent comfortable with lapply
you can still use a for
loop for the same result:
plot_list = list()
for (x in names(df)) {
plot_list[[x]] = ggplot(df[x], aes_string(x = x)) +
stat_ecdf(geom = "step")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.