Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nicely annotate a ggplot2 (manual)

Tags:

r

ggplot2

Using ggplot2 I normally use geom_text and something like position=jitter to annotate my plots.

However - for a nice plot I often finds it worthwhile to annotate manually. like below:

data2 <- structure(list(type = structure(c(5L, 1L, 2L, 4L, 3L, 5L, 1L,  2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L), .Label = c("EDS",  "KIU", "LAK", "MVH", "NA*"), class = "factor"), value = c(0.9,  0.01, 0.01, 0.09, 0, 0.8, 0.05, 0, 0.15, 0, 0.41, 0.04, 0.03,  0.52, 0, 0.23, 0.11, 0.02, 0.64, 0.01), time = c(3L, 3L, 3L,  3L, 3L, 6L, 6L, 6L, 6L, 6L, 15L, 15L, 15L, 15L, 15L, 27L, 27L,  27L, 27L, 27L), year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2007L,  2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,  2006L, 2006L, 2006L, 2006L, 2006L)), .Names = c("type", "value",  "time", "year"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L,  11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 24L), class = "data.frame") ggplot(data2, aes(x=time, y=value, group=type, col=type))+ geom_line()+ geom_point()+ theme_bw()+ annotate("text", x=6, y=0.9, label="this is a wrong color")+ annotate("text", x=15, y=0.6, label="this is a second annotation with a wrong color") 

The problem is, that I can't get the text annotations color to match the line color. I assume I could fix this with a manual scale, but I hope there is a better way?

like image 300
Andreas Avatar asked Mar 09 '10 13:03

Andreas


People also ask

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 I annotate a plot in R?

If you want to annotate your plot or figure with labels, there are two basic options: text() will allow you to add labels to the plot region, and mtext() will allow you to add labels to the margins. For the plot region, to add labels you need to specify the coordinates and the label.

What are annotations in R?

Source: R/annotation.r. annotate.Rd. This function adds geoms to a plot, but unlike typical a geom function, the properties of the geoms are not mapped from variables of a data frame, but are instead passed in as vectors.


2 Answers

I had a similar problem and solved it with JD Long answer. But as a results of ggplot2 updating to version 0.9.0 I noticed that all geom_text()calls rendered somewhat blurred on the plots.

Thanks to kohske I discovered that this code

ggplot(data2, aes(x=time, y=value, group=type, col=type))+ geom_line()+ geom_point()+ theme_bw() + geom_text(aes(7, .9, label="correct color", color="NA*")) + geom_text(aes(15, .6, label="another correct color!", color="MVH"))  

plots the geom_text nrow(data2)times!

The correct way for supplying data to geom_text is building a different data.frame holding coordinates, labels and colors for the strings you want to be plotted:

data2.labels <- data.frame(   time = c(7, 15),    value = c(.9, .6),    label = c("correct color", "another correct color!"),    type = c("NA*", "MVH")   )  ggplot(data2, aes(x=time, y=value, group=type, col=type))+   geom_line()+   geom_point()+   theme_bw() +   geom_text(data = data2.labels, aes(x = time, y = value, label = label)) 
like image 172
mbask Avatar answered Sep 29 '22 06:09

mbask


If you use geom_text() instead of annotate() you can pass a group color to your plot:

ggplot(data2, aes(x=time, y=value, group=type, col=type))+ geom_line()+ geom_point()+ theme_bw() + geom_text(aes(7, .9, label="correct color", color="NA*")) + geom_text(aes(15, .6, label="another correct color!", color="MVH"))  

So using annotate() it looks like this: alt text http://www.cerebralmastication.com/wp-content/uploads/2010/03/before.png

then after using geom_text() it looks like this: alt text http://www.cerebralmastication.com/wp-content/uploads/2010/03/after.png

like image 42
JD Long Avatar answered Sep 29 '22 05:09

JD Long