2013-01-16 3 views
13

私は3つの列を持つデータフレームaあります。そして、私はそのGeneName"G1"あるポイントを色や追加したい私はこの1点をカラーにし、ggplot2でアノテーションを追加しますか?

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

のような散布図を描く

GeneNameIndex1Index2

をその点の近くのテキストボックス、それを行う最も簡単な方法は何ですか?あなたはただ、その点を含むサブセットを作成し、そのプロットに追加することができ

答えて

17

何かこれはうまくいくはずです。あなたはxyの引数をgeom_text()に混乱させる必要があります。

library(ggplot2) 

highlight.gene <- "G1" 

set.seed(23456) 
a <- data.frame(GeneName = paste("G", 1:10, sep = ""), 
        Index1 = runif(10, 100, 200), 
        Index2 = runif(10, 100, 150)) 

a$highlight <- ifelse(a$GeneName == highlight.gene, "highlight", "normal") 
textdf <- a[a$GeneName == highlight.gene, ] 
mycolours <- c("highlight" = "red", "normal" = "grey50") 

a 
textdf 

ggplot(data = a, aes(x = Index1, y = Index2)) + 
    geom_point(size = 3, aes(colour = highlight)) + 
    scale_color_manual("Status", values = mycolours) + 
    geom_text(data = textdf, aes(x = Index1 * 1.05, y = Index2, label = "my label")) + 
    theme(legend.position = "none") + 
    theme() 

screenshot

+1

@Arunはい、確かにあなたは可能性があり、十分なされているだろう、本当に最小限例えば。データフレームは、複数のラベル(たとえば、ポイントG1とG7)に簡単に展開できるので、私はデータフレームを使いたいと思っていました。しかし、注釈を思い出すことは良いことです。 – SlowLearner

37

# 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 

注:誰もがアップ投票この質問を保持しているので、私はそれが読みやすくだろうと思いました。

関連する問題