[R] Need help with pointLabels()

Joshua Wiley jwiley.psych at gmail.com
Mon Nov 15 22:11:32 CET 2010

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.



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

More information about the R-help mailing list