Hello R-list, I am plotting a weighted linear regression in R. The points on my chart are also scaled to sample size, so some points are large, some are small. I have figured out everything I need using the plot() function: how to plot the points, scale them by sample size, weight the linear regression by sample size, plot that line, and plot the labels for the points. However, although the pointLabel() function assures that the labels do not overlap with each other, they still overlap with the larger points, and sometimes run off the side of the chart too. I have tried saving the plot as EPS, PDF, SVG and opening them in GIMP to try to move the text around, but GIMP does not recognize the text as text, so I cannot select the labels and move them off the points. I have tried various "offsets" too but that does not work either. So I need to either (a) print the labels on the plot so that they do not overlap the points or (b) be able to move the text around in the resulting image file. Any advice? Here is the code I am using. BTW, I have also tried ggplot2 but it has no function (that I am aware of) like pointLabels() to avoid label overlap. Please feel free to email me directly. postscript(file="fig_a.eps"); plot(x, y, xlab="X-axis", ylab="Y-axis", cex=0.02*sample_size, pch=21); abline(lm(y~x, weight=sample_size)) pointLabel(x, y, labels=Category, cex=1, doPlot=TRUE, offset=2.5); dev.off() Thank you, -craig.starger@gmail.com [[alternative HTML version deleted]]
What package is pointLabel (or is it pointLabels) in? giving a reproducible example includes stating packages other than the standard ones. What you are trying to do is not simple for general cases, some tools work better on some datasets, but others work better on other datasets. Some other tools that may help (but you will probably need to do some hand tweaking after using them, or basing your solution on these) could be: thigmophobe.labels in the plotrix package spread.labels in the plotrix package TkIdentify in the TeachingDemos package spread.labs in the TeachingDemos package -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Craig Starger > Sent: Monday, November 15, 2010 12:38 PM > To: r-help at r-project.org > Subject: [R] Need help with pointLabels() > > Hello R-list, > > I am plotting a weighted linear regression in R. The points on my chart > are > also scaled to sample size, so some points are large, some are small. I > have > figured out everything I need using the plot() function: how to plot > the > points, scale them by sample size, weight the linear regression by > sample > size, plot that line, and plot the labels for the points. However, > although > the pointLabel() function assures that the labels do not overlap with > each > other, they still overlap with the larger points, and sometimes run off > the > side of the chart too. I have tried saving the plot as EPS, PDF, SVG > and > opening them in GIMP to try to move the text around, but GIMP does not > recognize the text as text, so I cannot select the labels and move them > off > the points. I have tried various "offsets" too but that does not work > either. So I need to either (a) print the labels on the plot so that > they do > not overlap the points or (b) be able to move the text around in the > resulting image file. Any advice? Here is the code I am using. BTW, I > have > also tried ggplot2 but it has no function (that I am aware of) like > pointLabels() to avoid label overlap. Please feel free to email me > directly. > > postscript(file="fig_a.eps"); > plot(x, y, xlab="X-axis", ylab="Y-axis", cex=0.02*sample_size, pch=21); > abline(lm(y~x, weight=sample_size)) > pointLabel(x, y, labels=Category, cex=1, doPlot=TRUE, offset=2.5); > dev.off() > > Thank you, > > -craig.starger at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Hi Craig, As Greg pointed out, choose optimal locations for labels is tricky (side note for anyone reading pointLabel() comes from the maptools package). Inferring from your code, the labels you are plotting are the "Category" each point belongs to which suggests there may not be a huge amount of unique values. Assuming you have a reasonably small number of unique labels (say 10), what about colouring the points and adding a legend? This would be straightforward to code and completely sidesteps the locationing of labels issue. Of course, this can become quite clunky with numerous levels. You could do something similar with point shape, and you could even mix shape and color to convey more information in the same space. Cheers, Josh On Mon, Nov 15, 2010 at 11:37 AM, Craig Starger <craig.starger at gmail.com> wrote:> Hello R-list, > > I am plotting a weighted linear regression in R. The points on my chart are > also scaled to sample size, so some points are large, some are small. I have > figured out everything I need using the plot() function: how to plot the > points, scale them by sample size, weight the linear regression by sample > size, plot that line, and plot the labels for the points. However, although > the pointLabel() function assures that the labels do not overlap with each > other, they still overlap with the larger points, and sometimes run off the > side of the chart too. I have tried saving the plot as EPS, PDF, SVG and > opening them in GIMP to try to move the text around, but GIMP does not > recognize the text as text, so I cannot select the labels and move them off > the points. I have tried various "offsets" too but that does not work > either. So I need to either (a) print the labels on the plot so that they do > not overlap the points or (b) be able to move the text around in the > resulting image file. Any advice? Here is the code I am using. BTW, I have > also tried ggplot2 but it has no function (that I am aware of) like > pointLabels() to avoid label overlap. Please feel free to email me directly. > > postscript(file="fig_a.eps"); > plot(x, y, xlab="X-axis", ylab="Y-axis", cex=0.02*sample_size, pch=21); > abline(lm(y~x, weight=sample_size)) > pointLabel(x, y, labels=Category, cex=1, doPlot=TRUE, offset=2.5); > dev.off() > > Thank you, > > -craig.starger at gmail.com > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/