Подтвердить что ты не робот

Цвет одной точки и добавить аннотацию в ggplot2?

У меня есть dataframe a с тремя столбцами:

GeneName, Index1, Index2

Я рисую диаграмму рассеяния, подобную этой

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

Затем я хочу, чтобы цвет точки GeneName "G1" и добавить текстовое поле рядом с этой точкой, что может быть самым простым способом сделать это?

4b9b3361

Ответ 1

Что-то вроде этого должно работать. Возможно, вам придется обходиться с аргументами x и y до 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

Ответ 2

Вы можете создать подмножество, содержащее только эту точку, а затем добавить его в график:

# 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

ПРИМЕЧАНИЕ. Поскольку каждый продолжает голосование по этому вопросу, я думал, что мне будет легче читать.