我试图迭代地构建我的数据的所有可能的图表,每个表格中的每列都有颜色。
到目前为止,我有这样的代码:
# ----- next is a function taken from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ # --- not relevant to the question, my code is in the end of the snippet library(ggplot2) multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { library(grid) # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # If layout is NULL, then use 'cols' to determine layout if (is.null(layout)) { # Make the panel # ncol: Number of columns of plots # nrow: Number of rows needed, calculated from # of cols layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols)) } if (numPlots==1) { print(plots[[1]]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for (i in 1:numPlots) { # Get the i,j matrix positions of the regions that contain this subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } #---------------------------------------------------------------- temp23_before6 <- data.frame(TIME = c(1, 2, 3, 4, 5), VALUE = c(1, 2, 3, 4, 5), P = c(1, 2, 3, 2, 1), D = c(4, 5, 6, 7, 8)) i <- 1 p <- list() for (col in names(temp23_before6)) { l <-length(unique(temp23_before6[, col])) if (l < 20 && l > 1) { cc <- col p[[i]] <- ggplot(temp23_before6, aes(TIME, VALUE, colour=factor(temp23_before6[, cc]))) + geom_point() + labs(title=col) i <- i + 1 } } multiplot(plotlist = p, cols = as.integer(sqrt(i)))不幸的是cc由于关闭而没有改变,并且我收到的所有图都完全一样。 与其他语言一起使用的常用技巧 - 将col分配给本地变量 - 不起作用。 我如何使它在R中工作?
更新更新后的代码,以便可以在新的R env中运行该示例。 我期望这四个地块具有不同的颜色。 它们中的两个Time和Value应该是明显的单色,另外两个P和D应该有不同的颜色,由P = c(1, 2, 3, 2, 1), D = c(4, 5, 6, 7, 8) 1,2,3,2,1)确定P = c(1, 2, 3, 2, 1), D = c(4, 5, 6, 7, 8) ,所以D应该有5种不同的颜色,而P应该只有3种
I'm trying to iteratively built all possible plots of my data, colored by each of the column in a table.
So far I have this code:
# ----- next is a function taken from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ # --- not relevant to the question, my code is in the end of the snippet library(ggplot2) multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { library(grid) # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # If layout is NULL, then use 'cols' to determine layout if (is.null(layout)) { # Make the panel # ncol: Number of columns of plots # nrow: Number of rows needed, calculated from # of cols layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols)) } if (numPlots==1) { print(plots[[1]]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for (i in 1:numPlots) { # Get the i,j matrix positions of the regions that contain this subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } #---------------------------------------------------------------- temp23_before6 <- data.frame(TIME = c(1, 2, 3, 4, 5), VALUE = c(1, 2, 3, 4, 5), P = c(1, 2, 3, 2, 1), D = c(4, 5, 6, 7, 8)) i <- 1 p <- list() for (col in names(temp23_before6)) { l <-length(unique(temp23_before6[, col])) if (l < 20 && l > 1) { cc <- col p[[i]] <- ggplot(temp23_before6, aes(TIME, VALUE, colour=factor(temp23_before6[, cc]))) + geom_point() + labs(title=col) i <- i + 1 } } multiplot(plotlist = p, cols = as.integer(sqrt(i)))Unfortunately cc is not changed due to closure, and I receive all the plots exactly the same. The usual trick which works with other languages - assign col to a local variable - doesn't work. How do I make it work in R?
Update Updated code so the example can be ran in a new R env. I expect these four plots to be of different color. The two of them Time and Value should be obviously of single color, and the other two P and D should have different colors, determined by P = c(1, 2, 3, 2, 1), D = c(4, 5, 6, 7, 8), so D should have 5 different colors, and P should have only 3
最满意答案
这个问题更多的是在强制性的内部进行编程。 我们做了两个改变,1)添加aes_string以允许在ggplot调用中进行评估,并且2)清除由着色列命名的图例:
p[[i]] <- ggplot(temp23_before6, aes_string("TIME", "VALUE", colour=factor(temp23_before6[,cc]))) + geom_point() + labs(title=col) + scale_colour_discrete(name=cc)
This problem has more to do with programming within the hadlyverse. We make two changes, 1) add aes_string to allow evaluation within the ggplot call, and 2) clean up the legend to be named by the coloring column:
p[[i]] <- ggplot(temp23_before6, aes_string("TIME", "VALUE", colour=factor(temp23_before6[,cc]))) + geom_point() + labs(title=col) + scale_colour_discrete(name=cc)更多推荐
plots,<-,layout,col,电脑培训,计算机培训,IT培训"/> <meta name="descri
发布评论