Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color one point and add an annotation in ggplot2?

Tags:

I have a dataframe a with three columns :

GeneName, Index1, Index2

I draw a scatterplot like this

ggplot(a, aes(log10(Index1+1), Index2)) +geom_point(alpha=1/5) 

Then I want to color a point whose GeneName is "G1" and add a text box near that point, what might be the easiest way to do it?

like image 786
Hanfei Sun Avatar asked Jan 16 '13 04:01

Hanfei Sun


People also ask

How do you annotate a point 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.


1 Answers

You could create a subset containing just that point and then add it to the plot:

# create the subset g1 <- subset(a, GeneName == "G1")  # plot the data ggplot(a, aes(log10(Index1+1), Index2)) + geom_point(alpha=1/5) +  # this is the base plot   geom_point(data=g1, colour="red") +  # this adds a red point   geom_text(data=g1, label="G1", vjust=1) # this adds a label for the red point 

NOTE: Since everyone keeps up-voting this question, I thought I would make it easier to read.

like image 107
rrs Avatar answered Sep 21 '22 06:09

rrs