本文介绍了在R的循环中的barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的问题: #data 1: lab1< - 1:10 $ b $ (c(0,1,4,...,1),其中每一个表示一个或多个表达式, 5,6,8,10,11,12,13),3) myd< - data.frame(group,label,avar) #data 2 fillcol group1 #这个变量将被用来填充条的颜色 filld< - data.frame(group1,fillcol) #现在绘制 par(mfrow = c(3,1 )) par(mar = c(2.5,1,2.5,1)) #plot1 myd1 filld1 blues< - colorRampPalette(c(yellow,blue)) barplot(as.matrix(diff myd1 $ avar)),horiz = T,col = blues(10)[10 * filld1 $ fillcol], axes = F,xlab =Mark) axis(1,labels = myd $标签,at = myd $ avar)轴(3,labels = myd $ avar,at = myd $ avar) 虽然这个示例da taset,我有很多变数,我想让这个过程自动化。 for(i in 1:length(unique(myd $ group))){ par(mfrow = c (i,1)) par(mar = c(2.5,1,2.5,1)) myd [i]< - myd [myd $ group == i,] filld [i]< - filld [filld $ group1 == i,] blues< - colorRampPalette(c(yellow,blue)) barplot(as.matrix(diff myd [i] $ avar)),horiz = T, col = blues(10)[10 * filld1 $ fillcol],axes = F,xlab =Mark) axis(1,labels, = myd [i] $ label,at = myd [i] $ avar) axis(3,labels = myd [i] $ avar,at = myd [i] $ avar)} dim(data)中的错误< - dim:尝试在NULL 上设置属性另外:警告消息: 1:在[[提供了3个变量来替换1个变量 2:在[[提供了2个变量来替换1个变量 编辑:我想创建一个循环s o我在循环中创建了多个图表: PS:我是R和stackoverflow的新手。如果问题不恰当,请原谅我,尽管我阅读了指南并试图坚持下去。 解决方案在一个像这样的函数中,你应该能够使它工作并被多次调用。 colbarplot< - 函数(group){ myd1< - myd [myd $ group == group,] filld1< - filld [filld $ group1 == group,] blues< ; colorRampPalette(c(yellow,blue)) barplot(as.matrix(diff(myd1 $ avar)),horiz = T, col = blues(10) (1,labels = myd $ label,at = myd $ avar) axis(3,labels = myd),轴= F,xlab =Mark)轴$ avar,at = myd $ avar)} par(mfrow = c(3,1)) par(mar = c(2.5,1,2.5,1 )) sapply(unique(myd $ group),function(x)colbarplot(x)) 这会在一张页面上显示多张图。 Here is my question:#data 1:lab1 <- 1:10group <- rep(1:3, each = length (lab1))label <- rep(lab1, 3)avar <- rep(c(0, 1, 4, 5, 6, 8, 10, 11, 12, 13), 3)myd <- data.frame (group, label, avar)# data 2fillcol <- rep(rnorm(length(lab1)-1, 0.5, 0.2), 3)group1 <- rep(1:3, each = length(fillcol)/3) # this variable will be used to fill color in bars filld <- data.frame(group1, fillcol)# now plottingpar(mfrow = c(3, 1)) par(mar = c(2.5, 1, 2.5, 1))#plot1myd1 <- myd[myd$group ==1,]filld1 <- filld[filld$group1 ==1,]blues <- colorRampPalette(c("yellow", "blue"))barplot(as.matrix(diff(myd1$avar)), horiz=T, col=blues(10)[10* filld1$fillcol], axes=F, xlab="Mark")axis(1, labels=myd$label, at=myd$avar)axis(3, labels=myd$avar, at=myd$avar)Although this sample dataset, I have many variables and I want to automate this process. for (i in 1:length(unique(myd$group))){ par(mfrow = c(i, 1)) par(mar = c(2.5, 1, 2.5, 1)) myd[i] <- myd[myd$group ==i,] filld[i] <- filld[filld$group1 ==i,] blues <- colorRampPalette(c("yellow", "blue")) barplot(as.matrix(diff(myd[i]$avar)), horiz=T, col=blues(10)[10* filld1$fillcol], axes=F, xlab="Mark") axis(1, labels=myd[i]$label, at=myd[i]$avar) axis(3, labels=myd[i]$avar, at=myd[i]$avar) } Error in dim(data) <- dim : attempt to set an attribute on NULLIn addition: Warning messages:1: In `[<-.data.frame`(`*tmp*`, i, value = list(group = c(1L, 1L, 1L, : provided 3 variables to replace 1 variables2: In `[<-.data.frame`(`*tmp*`, i, value = list(group1 = c(1L, 1L, : provided 2 variables to replace 1 variablesEdits: I want to create a loop so that I create multiple graphs within the loop:PS: I am new to R and stackoverflow. Please excuse me if the question is not appropriate, although I read the guidelines and try to adhere to it 解决方案 If you wrap it in a function like this, you should be able to get it to work and be called multiple times.colbarplot <- function(group) { myd1 <- myd[myd$group == group,] filld1 <- filld[filld$group1 == group,] blues <- colorRampPalette(c("yellow", "blue")) barplot(as.matrix(diff(myd1$avar)), horiz=T, col=blues(10)[10* filld1$fillcol], axes=F, xlab="Mark") axis(1, labels=myd$label, at=myd$avar) axis(3, labels=myd$avar, at=myd$avar)}par(mfrow = c(3, 1))par(mar = c(2.5, 1, 2.5, 1))sapply(unique(myd$group),function(x) colbarplot(x))This will give the multiple plots on one page you are after. 这篇关于在R的循环中的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 16:03