Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

r - Color one point and add an annotation in ggplot2?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...