Re: [R] adding value labels on Interaction Plot
On Wed, Mar 4, 2009 at 10:52 AM, Dimitri Liakhovitski ld7...@gmail.com wrote: Hello - and sorry for what might look like a simple graphics question. I am building an interaction plot for d: d=data.frame(xx=c(3,3,2,2,1,1),yy=c(4,3,4,3,4,3),zz=c(5.1,4.4,3.5,3.3,-1.1,-1.3)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx, d$yy, d$zz, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label, ylab=Y Label, main=Chart Label) I am trying and not succeeding in adding Y values (value labels in Excel speak) near the data points on 3 lines of the graph. I understand that I might have to use text. But how do I tell text to use the actual coordinates of the dots on the lines? Thank you very much! I'm not understanding your trouble, exactly. I had not heard of interaction.plot before and so I've run your code and it is an interesting function. Thanks for providing the working example. I can help you with the text. It is easy to add text. A commmands like text( 1.3, 1, whatever, pos=3) will put the word whatever on top of coordinates x and y. (you leave out pos=3 and R centes the text on the coordinates). If you need to find out x , y before running that, you can. the locator function will return coordinates. Run locator(1) and then left click on a point in the graph. Coordinates will pop out on the screen. And you can make the text placement depend on locator text(locator(1), whatever, pos=3) I don't think you want to do that work interactively, however. It can be automated. You can add a column of names in your data frame and more or less automate the plotting as well. I did this to test. mylabels - c(A,B,C,D,E,F) text(d$xx,d$zz, mylabels, pos=3) This almost works perfectly, but it plops the labels in the wrong spots. I'd like to change the command so that the position of the text for the red line would be on the right, while the position of the text for the blue line is on the left. It appears to me your variable yy is the one that separates the 2 lines. Correct? I observe: as.numeric(d$yy) [1] 2 1 2 1 2 1 We want the blue ones on the left, for them we need pos=2. For the others, we want pos=4 Ach. I tried this text( d$xx, d$zz, mylabels, pos=2*as.numeric(d$yy)) but it comes out backward. So how about this: text( d$xx, d$zz, mylabels, pos=as.numeric(d$yy)) That positions the red ones below the line and the blue ones to the left. That doesn't look too bad to me. Anyway, I think you get the idea. If you wanted to simply stop plotting the circles, and put the letters right on the spot, that would be easy as well. -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas __ 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] adding value labels on Interaction Plot
See if this helps. After your code, submit this to R: with(d, text(xx[xx==3],zz[xx==3],paste(3, ,zz[xx==3]))) After that has convinced you that xx and zz are being used properly, you can try the more general approach: with(d, text(xx,zz,paste(xx, , , zz))) I would have used ZZ rather than Y Label on the y axis, because yy is being used as a grouping parameter and the plotted value is really zz -- David Winsemius On Mar 4, 2009, at 11:52 AM, Dimitri Liakhovitski wrote: Hello - and sorry for what might look like a simple graphics question. I am building an interaction plot for d: d = data .frame (xx=c(3,3,2,2,1,1),yy=c(4,3,4,3,4,3),zz=c(5.1,4.4,3.5,3.3,-1.1,-1.3)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx, d$yy, d$zz, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label, ylab=Y Label, main=Chart Label) I am trying and not succeeding in adding Y values (value labels in Excel speak) near the data points on 3 lines of the graph. I understand that I might have to use text. But how do I tell text to use the actual coordinates of the dots on the lines? Thank you very much! -- Dimitri Liakhovitski MarketTools, Inc. dimitri.liakhovit...@markettools.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. __ 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] adding value labels on Interaction Plot
Thank you, David, however, I am not sure this approach works. Let's try it again - I slightly modifed d to make it more clear: d=data.frame(xx=c(1,1,1,1,2,2,2,2,3,3,3,3),yy=c(3,3,4,4,3,3,4,4,3,3,4,4),zz=c(-1.1,-1.3,0,0.6,-0.5,1,3.3,-1.3,4.4,3.5,5.1,3.5)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx, d$yy, d$zz, fun=mean, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label (level of xx), ylab=Y Label (level of zz), main=Chart Label) grid(nx=NA, ny=NULL,col = lightgray, lty = dotted, lwd = par(lwd), equilogs = TRUE) legend(bottomright,c(3,4),bty=n,lty=c(1,2),lwd=2, pch=c(18,24),col=c(red,blue),title=Level of yy) The dots on both lines show means on zz for a given combination of factors xx and yy. # If I add this line: with(d, text(xx,zz,paste(zz))) It adds the zz values for ALL data points in d - instead of the means that are shown on the graph... Any advice? Dimitri d=data.frame(xx=c(3,3,3,2,2,2,1,1,1),yy=c(4,3,4,3,4,3,4,3,4),zz=c(5.1,4.4,3.5,3.3,-1.1,-1.3,0,-0.5,0.6)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx, d$yy, d$zz, fun=mean, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label, ylab=Y Label, main=Chart Label) grid(nx=NA, ny=NULL,col = lightgray, lty = dotted, lwd = par(lwd), equilogs = TRUE) legend(bottomright,c(0.25,0.50,0.75),bty=n,lty=c(1,2,3),lwd=2, pch=c(18,24,22),col=c(PrimaryColors[c(6,4)],SecondaryColors[c(4)]),title=R Squared) On Wed, Mar 4, 2009 at 2:06 PM, David Winsemius dwinsem...@comcast.net wrote: See if this helps. After your code, submit this to R: with(d, text(xx[xx==3],zz[xx==3],paste(3, ,zz[xx==3]))) After that has convinced you that xx and zz are being used properly, you can try the more general approach: with(d, text(xx,zz,paste(xx, , , zz))) I would have used ZZ rather than Y Label on the y axis, because yy is being used as a grouping parameter and the plotted value is really zz -- David Winsemius On Mar 4, 2009, at 11:52 AM, Dimitri Liakhovitski wrote: Hello - and sorry for what might look like a simple graphics question. I am building an interaction plot for d: d=data.frame(xx=c(3,3,2,2,1,1),yy=c(4,3,4,3,4,3),zz=c(5.1,4.4,3.5,3.3,-1.1,-1.3)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx, d$yy, d$zz, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label, ylab=Y Label, main=Chart Label) I am trying and not succeeding in adding Y values (value labels in Excel speak) near the data points on 3 lines of the graph. I understand that I might have to use text. But how do I tell text to use the actual coordinates of the dots on the lines? Thank you very much! -- Dimitri Liakhovitski MarketTools, Inc. dimitri.liakhovit...@markettools.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. -- Dimitri Liakhovitski MarketTools, Inc. dimitri.liakhovit...@markettools.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] adding value labels on Interaction Plot
I'll reply to my own post - to make sure no one wastes his/her time on that. I was able to solve the problem only after I modified the original function interaction.plot (see below). All I did I added one line before the final } - asking it to return the means on the numeric (dependent) variable. After that, the following code worked: d=data.frame(xx=c(1,1,1,1,2,2,2,2,3,3,3,3),yy=c(3,3,4,4,3,3,4,4,3,3,4,4),zz=c(-1.1,-1.3,0,0.6,-0.5,1,3.3,-1.3,4.4,3.5,5.1,3.5)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) coordinates-modified.interaction.plot(d$xx, d$yy, d$zz, fun=mean, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label (level of xx), ylab=Y Label (level of zz), main=Chart Label) grid(nx=NA, ny=NULL,col = lightgray, lty = dotted, lwd = par(lwd), equilogs = TRUE) legend(bottomright,c(3,4),bty=n,lty=c(1,2),lwd=2, pch=c(18,24),col=c(red,blue),title=Level of yy) coordinates-as.data.frame(coordinates) str(coordinates) for(i in 1:length(coordinates)) { text(x=as.numeric(row.names(coordinates)),y=coordinates[[i]],labels=coordinates[[i]],pos=3) } ### Modified interaction.plot function ### modified.interaction.plot=function (x.factor, trace.factor, response, fun = mean, type = c(l, p, b), legend = TRUE, trace.label = deparse(substitute(trace.factor)), fixed = FALSE, xlab = deparse(substitute(x.factor)), ylab = ylabel, ylim = range(cells, na.rm = TRUE), lty = nc:1, col = 1, pch = c(1:9, 0, letters), xpd = NULL, leg.bg = par(bg), leg.bty = n, xtick = FALSE, xaxt = par(xaxt), axes = TRUE, ...) { ylabel - paste(deparse(substitute(fun)), of , deparse(substitute(response))) type - match.arg(type) cells - tapply(response, list(x.factor, trace.factor), fun) nr - nrow(cells) nc - ncol(cells) xvals - 1:nr if (is.ordered(x.factor)) { wn - getOption(warn) options(warn = -1) xnm - as.numeric(levels(x.factor)) options(warn = wn) if (!any(is.na(xnm))) xvals - xnm } xlabs - rownames(cells) ylabs - colnames(cells) nch - max(sapply(ylabs, nchar, type = width)) if (is.null(xlabs)) xlabs - as.character(xvals) if (is.null(ylabs)) ylabs - as.character(1:nc) xlim - range(xvals) xleg - xlim[2] + 0.05 * diff(xlim) xlim - xlim + c(-0.2/nr, if (legend) 0.2 + 0.02 * nch else 0.2/nr) * diff(xlim) matplot(xvals, cells, ..., type = type, xlim = xlim, ylim = ylim, xlab = xlab, ylab = ylab, axes = axes, xaxt = n, col = col, lty = lty, pch = pch) if (axes xaxt != n) { axisInt - function(x, main, sub, lwd, bg, log, asp, ...) axis(1, x, ...) mgp. - par(mgp) if (!xtick) mgp.[2] - 0 axisInt(1, at = xvals, labels = xlabs, tick = xtick, mgp = mgp., xaxt = xaxt, ...) } if (legend) { yrng - diff(ylim) yleg - ylim[2] - 0.1 * yrng if (!is.null(xpd) || { xpd. - par(xpd) !is.na(xpd.) !xpd. (xpd - TRUE) }) { op - par(xpd = xpd) on.exit(par(op)) } text(xleg, ylim[2] - 0.05 * yrng, paste( , trace.label), adj = 0) if (!fixed) { ord - sort.list(cells[nr, ], decreasing = TRUE) ylabs - ylabs[ord] lty - lty[1 + (ord - 1)%%length(lty)] col - col[1 + (ord - 1)%%length(col)] pch - pch[ord] } legend(xleg, yleg, legend = ylabs, col = col, pch = if (type %in% c(p, b)) pch, lty = if (type %in% c(l, b)) lty, bty = leg.bty, bg = leg.bg) } invisible() return(cells) } On Wed, Mar 4, 2009 at 3:48 PM, Dimitri Liakhovitski ld7...@gmail.com wrote: Thank you, David, however, I am not sure this approach works. Let's try it again - I slightly modifed d to make it more clear: d=data.frame(xx=c(1,1,1,1,2,2,2,2,3,3,3,3),yy=c(3,3,4,4,3,3,4,4,3,3,4,4),zz=c(-1.1,-1.3,0,0.6,-0.5,1,3.3,-1.3,4.4,3.5,5.1,3.5)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx, d$yy, d$zz, fun=mean, type=b, col=c(red,blue), legend=F, lty=c(1,2), lwd=2, pch=c(18,24), xlab=X Label (level of xx), ylab=Y Label (level of zz), main=Chart Label) grid(nx=NA, ny=NULL,col = lightgray, lty = dotted, lwd = par(lwd), equilogs = TRUE) legend(bottomright,c(3,4),bty=n,lty=c(1,2),lwd=2, pch=c(18,24),col=c(red,blue),title=Level of yy) The dots on both lines show means on zz for a given combination of factors xx and yy. # If I add this line: with(d, text(xx,zz,paste(zz))) It adds the zz values for ALL data points in d - instead of the means that are shown on the graph... Any advice? Dimitri d=data.frame(xx=c(3,3,3,2,2,2,1,1,1),yy=c(4,3,4,3,4,3,4,3,4),zz=c(5.1,4.4,3.5,3.3,-1.1,-1.3,0,-0.5,0.6)) d[[1]]-as.factor(d[[1]]) d[[2]]-as.factor(d[[2]]) print(d) interaction.plot(d$xx,