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?
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.)
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.
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.
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
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.
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 :
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With