Re: [R] Frequencies from a matrix - spider from frequencies
Jim Lemon wrote: Yes, I realized that I had forgotten to require(plotrix) after I sent the message. From your example, you might also want to look at the diamondplot function, also in plotrix. Jim, thanks for the hint to diamondplot. It is much closer natively to what I wanted to do, and simple to use. Hats off!: Just entering the data frame produces a quick print. However, it fails to make sense w.r.t. units and values here. I use the example data and code given in ?diamondplot: data(mtcars) mysubset<-mtcars[substr(dimnames(mtcars)[[1]],1,1)=="M",c("mpg","hp","wt","disp")] diamondplot(mysubset) and get a plot (I think I can't attach it here?), with, e.g. hp and disp crossing the Maserati radial axis at 17, wt at 15 and mpg at 10. The actual data row, though, is mpg hpwt disp Maserati Bora 15.0 335 3.570 301.0 Looking closer, the plot seems to arbitrarily scale all values (columns) to a(n arbitrary?) maximum of '17'. And when I print my data (submitted earlier), the same happens: all responses are scaled to 17 as the highest in each category. From that point of view, diamondplot is not that useful. How can I force it not to scale arbitrarily, but print the actual numbers? Uwe __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Frequencies from a matrix - spider from frequencies
On 03/16/2010 02:43 AM, Uwe Dippel wrote: ... Thanks, Jim, this one works (with your random data)! (In case anyone reads and wants this as well, package plotrix provides radial.plot.) The overlap is normal, because it's random data. When the respondents have a less random streak of answering (and they do), this plot will show some continuity throughout the questions; and identify those questions clearly, which fall outside of this continuity. This is why I prefer to use it here. Uwe Yes, I realized that I had forgotten to require(plotrix) after I sent the message. From your example, you might also want to look at the diamondplot function, also in plotrix. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Frequencies from a matrix - spider from frequencies
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Uwe Dippel > Sent: Monday, March 15, 2010 8:27 AM > To: Dennis Murphy > Cc: r-help@r-project.org > Subject: Re: [R] Frequencies from a matrix - spider from frequencies > > Dennis Murphy wrote: > > > > The first part is straightforward. You didn't supply example data, > > but it's easy to generate it oneself: > > > > likmat <- matrix(sample(1:5, 310, replace = TRUE), nrow = 31) > > dim(likmat) > > # [1] 31 10 > > frq <- t(apply(likmat, 2, table)) # 10 x 5 matrix, > questions in rows > > Yours works, mine doesn't. As I wrote, newbie. > The difference here is the naming of rows and columns. Your > matrix looks > like > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,]233452122 2 > [2,]213132152 4 > while mine goes like this: >V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 > 1 1 2 3 4 5 6 7 8 9 10 > 2 4 4 4 3 4 3 4 4 3 4 You probably have a "data.frame", not a "matrix". Look at class(yourData) or str(yourData) to verify this. Both are 2 dimensional structures but they differ in many ways. This is why the posting guidelines ask that you send a way another user can recreate your data objects by copying and pasting R commands from your mail to an R session. However, the reason you and Dennis got different results is probably that in your example not every column had the same set of unique values ("levels") in it, hence the tables produced for each column have a different number of entries and apply() did not "simplify" its output to be a matrix. You were lucky that you had different numbers of levels in the various columns: if there had been the same number then it would have silently pasted together misaligned outputs. E.g., > d1 <- data.frame(x=c(11,12,12), y=c(12,13,13)) > apply(d1, 2, table) # incorrect results x y [1,] 1 1 [2,] 2 2 One way to deal with this is tell table() to use the same set of levels to tabulate for each column as in > apply(d1, 2, function(column)table(factor(column, levels=11:13))) x y 11 1 0 12 2 1 13 0 2 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > and then the 'apply' gets me some > > apply(m_learn, 2, table) > $V1 > 1 3 4 5 > 3 5 12 9 > $V2 > respectively > t(apply(m_learn, 2, table)) > V1V2V3V4V5V6 >V7 > [1,] Integer,4 Integer,5 Integer,4 Integer,5 Integer,5 > Integer,5 Integer,6 > V8V9V10 > [1,] Integer,6 Integer,5 Integer,5 > , while your matrix results exactly in what I was looking for. > > I have attached the data as file, hoping for it to go through. > > > > > > > > > The stars() function allows one to generate spider/radar > plots as you > > requested. Since there are several forms that it can take, I suggest > > you run the example: > > > > example(stars) > > > > and mimic the one(s) you want. > > Neither was what I had hoped for. (I attach the sample, > hoping for it to > go through. It is made with gnumeric.) > > Thanks very much anyway, I really appreciate your help! > > Uwe > > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Frequencies from a matrix - spider from frequencies
Jim Lemon wrote: Hi Uwe, Here's one way to get your spider plot: ld1<-matrix(sample(1:5,310,TRUE),nrow=31) ld2<-apply(ld1,2,table) radial.plot(ld2,line.col=2:6,rp.type="p", radial.pos=seq(0,9*pi/5,by=pi/5), labels=paste("Q",1:10,sep=""),start=pi/2, clockwise=TRUE,main="Frequency of response by question") par(xpd=TRUE) legend(8,12,1:5,col=2:6,lty=1) par(xpd=FALSE) Quite a bit of overlap on the polygons, however. Thanks, Jim, this one works (with your random data)! (In case anyone reads and wants this as well, package plotrix provides radial.plot.) The overlap is normal, because it's random data. When the respondents have a less random streak of answering (and they do), this plot will show some continuity throughout the questions; and identify those questions clearly, which fall outside of this continuity. This is why I prefer to use it here. Uwe __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Frequencies from a matrix - spider from frequencies
On 03/15/2010 06:24 PM, Uwe Dippel wrote: ... And, now somewhat less of the ordinary, a spider/radar showing the number of responses for each question (circular axis, 10), with the frequencies as radial axes. That is, 5 polygons showing the frequencies of the responses per each question. Hi Uwe, Here's one way to get your spider plot: ld1<-matrix(sample(1:5,310,TRUE),nrow=31) ld2<-apply(ld1,2,table) radial.plot(ld2,line.col=2:6,rp.type="p", radial.pos=seq(0,9*pi/5,by=pi/5), labels=paste("Q",1:10,sep=""),start=pi/2, clockwise=TRUE,main="Frequency of response by question") par(xpd=TRUE) legend(8,12,1:5,col=2:6,lty=1) par(xpd=FALSE) Quite a bit of overlap on the polygons, however. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Frequencies from a matrix - spider from frequencies
Uwe Dippel-2 wrote: > > Here comes my 'problem', over which I have sweated for the last 2 hours: > My data are of a matrix 10x31, Likert Scale (1-5). 10 questions, 31 > respondents. Now, I want to display the frequencies per question. I have > not found any better (any more simple) than > for (in in 1.10) print (table(learn[,i])) > Dennis has shown one way to do it. I personally prefer to arrange the data in the "long" format shown below from the beginning, because it is much more flexible when I want to derive summaries and plot the data: nsubj = 5 nquest= 4 d = matrix(as.integer(runif(nsubj*nquest,1,6)),nrow=nquest) colnames(d) = paste("subj",1:nsubj,sep="") rownames(d) = paste("quest",1:nquest,sep="") # These data are in the wide format # Convert data to the "long" format. It is much more flexible, # for example when you have missing data, and is the format of choice # when data are stored in a database. # dframe = data.frame( quest = rep(rownames(d),nsubj), subj = rep(colnames(d), nquest), resp = as.vector(d) ) dframe # Now we have the data in the long format, and the world is our limit # Give it a first try with xtabs xtabs(resp~quest+subj,data=dframe) # oops, not that, that is the original # Try ftable: looks good ftable(resp~quest,data=dframe) For the spider see ?star or http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=63 Dieter -- View this message in context: http://n4.nabble.com/Frequencies-from-a-matrix-spider-from-frequencies-tp1593012p1593062.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Frequencies from a matrix - spider from frequencies
Hi: The first part is straightforward. You didn't supply example data, but it's easy to generate it oneself: likmat <- matrix(sample(1:5, 310, replace = TRUE), nrow = 31) dim(likmat) # [1] 31 10 frq <- t(apply(likmat, 2, table)) # 10 x 5 matrix, questions in rows As for the plot, ?stars The stars() function allows one to generate spider/radar plots as you requested. Since there are several forms that it can take, I suggest you run the example: example(stars) and mimic the one(s) you want. HTH, Dennis On Mon, Mar 15, 2010 at 12:24 AM, Uwe Dippel wrote: > First of all, I really like R! Still being a newbie, I find things (the > difficult ones) to be very simple. > Alas, some 'simple' things still escape me. (Maybe the tutorials are often > too much focused on the 'difficult' items??) > > Here comes my 'problem', over which I have sweated for the last 2 hours: > My data are of a matrix 10x31, Likert Scale (1-5). 10 questions, 31 > respondents. Now, I want to display the frequencies per question. I have not > found any better (any more simple) than > for (in in 1.10) print (table(learn[,i])) > And then, still, the scale is printed 10 times as well. I am sure, there is > a better function, but I didn't find one. > Actually, I would want the scale once, atop ('names'), and then the > 10(questions) * 5 (length.of.scale) thereunder, like >1 2 3 4 5 > 1 3 4 2 1 2 > 2 5 9 2 1 4 > 3 4 4 6 1 3 > > > And, now somewhat less of the ordinary, a spider/radar showing the number > of responses for each question (circular axis, 10), with the frequencies as > radial axes. That is, 5 polygons showing the frequencies of the responses > per each question. > > Any help is appreciated, and my excuses for asking a simple question, > > Uwe > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.