如何打击R的封闭?(How to fight closures in R?)

编程入门 行业动态 更新时间:2024-10-28 04:28:08
如何打击R的封闭?(How to fight closures in R?)

我试图迭代地构建我的数据的所有可能的图表,每个表格中的每列都有颜色。

到目前为止,我有这样的代码:

# ----- 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)

enter image description here

更多推荐

plots,<-,layout,col,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-07-27 14:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:fight   closures

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!