着色一点并在ggplot2中添加注释?

时间:2021-12-07 22:55:26

I have a dataframe a with three columns :

我有一个包含三列的数据框:

GeneName, Index1, Index2

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?

然后我想为GeneName为“G1”的点着色并在该点附近添加一个文本框,这可能是最简单的方法吗?

2 个解决方案

#1


17  

Something like this should work. You may need to mess around with the x and y arguments to geom_text().

这样的事情应该有效。您可能需要弄乱geom_text()的x和y参数。

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()

着色一点并在ggplot2中添加注释?

#2


42  

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.

注意:由于每个人都在继续投票这个问题,我想我会更容易阅读。

#1


17  

Something like this should work. You may need to mess around with the x and y arguments to geom_text().

这样的事情应该有效。您可能需要弄乱geom_text()的x和y参数。

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()

着色一点并在ggplot2中添加注释?

#2


42  

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.

注意:由于每个人都在继续投票这个问题,我想我会更容易阅读。