Color one point and add an annotation in ggplot2?

后端 未结 2 895
野的像风
野的像风 2020-12-04 19:58

I have a dataframe a with three columns :

GeneName, Index1, Index2

I draw a scatterplot like this

2条回答
  •  失恋的感觉
    2020-12-04 20:46

    Something like this should work. You may need to mess around with the x and y arguments to 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

提交回复
热议问题