Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cleanly label the points in a simple ggplot2 scatterplot?

Tags:

r

ggplot2

See the examples from http://had.co.nz/ggplot2/geom_text.html; they're pretty terrible. Labels overlap each other, run outside the plot, etc.

I thought directlabels might help, but it doesn't really:

direct.label(qplot(wt,mpg,data=mtcars,colour=rownames(mtcars)))

Manually positioning each label is tedious. Hoping there's something that makes labels a bit more usable. Anything that might fit the bill?

like image 464
Yang Avatar asked Jun 09 '11 01:06

Yang


People also ask

How do you label points on a scatterplot in R?

To add the labels, we have text() , the first argument gives the X value of each point, the second argument the Y value (so R knows where to place the text) and the third argument is the corresponding label. The argument pos=1 is there to tell R to draw the label underneath the point; with pos=2 (etc.)

How do I mark points in ggplot2?

To add labels at specified points use annotate() with annotate(geom = "text", ...) or annotate(geom = "label", ...) . To automatically position non-overlapping text labels see the ggrepel package.

How do you add data point labels to Ggplot?

To put labels directly in the ggplot2 plot we add data related to the label in the data frame. Then we use functions geom_text() or geom_label() to create label beside every data point. Both the functions work the same with the only difference being in appearance.

How to label points in a scatter plot in R?

Label points in the scatter plot The function geom_text () can be used : ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + geom_text(label=rownames(mtcars)) Read more on text annotations : ggplot2 - add texts to a plot

How to add regression lines to a scatter plot using Ggplot2?

The functions below can be used to add regression lines to a scatter plot : geom_abline () has been already described at this link : ggplot2 add straight lines to a plot. Only the function geom_smooth () is covered in this section. method : smoothing method to be used.

How to change the shape of points in a scatter plot?

The color, the size and the shape of points can be changed using the function geom_point () as follow : Note that, the size of the points can be controlled by the values of a continuous variable as in the example below. The functions below can be used to add regression lines to a scatter plot :

What are X and Y in an xyplot?

The variables x and y contain numeric values for an xyplot and the variable label contains the names for the points of the plot. This Example illustrates how to draw labels to a plot created with the basic installation of the R programming language.


1 Answers

hope it's not too late for the answer. I don't now about ggplot2, I used normal scatterplots using plot(). I tried many labelling algorithms, and the best was pointLabel() from package maptools with wrapping the labels by spaces :-)

pointLabel(x, y, labels = paste("  ", point_names, "  ", sep="")

I tried thigmophobe.labels() from package plotrix, textxy() from package calibrate or standard text(), but I think pointLabel() was best.

But anyway for my purpose I ended up to simply use identify() and I just clicked the points for which I wanted the label - very useful, you just click the outliers and some interesting points that's it!!

Tomas

like image 145
Tomas Avatar answered Nov 12 '22 20:11

Tomas