2013-01-16 168 views
13

我有一个数据帧a有三列:为ggplot2添加一个注释并添加一个注释?

GeneNameIndex1Index2

我得出这样的

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

散点图然后我想颜色的点,其GeneName"G1"并添加在这一点附近的文本框中,可能最简单的方法是什么?

回答

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 

注:由于每个人都保持了投票权这个问题,我想我会更容易阅读。