Han*_*Sun 18 r ggplot2 dataframe
我有一个a
包含三列的数据框:
GeneName
,Index1
,Index2
我画了一个像这样的散点图
ggplot(a, aes(log10(Index1+1), Index2)) +geom_point(alpha=1/5)
Run Code Online (Sandbox Code Playgroud)
然后我想颜色的点,其GeneName
是"G1"
添加在那一点附近一个文本框,可能是什么做的最简单的方法?
rrs*_*rrs 49
您可以创建仅包含该点的子集,然后将其添加到绘图中:
# 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
Run Code Online (Sandbox Code Playgroud)
注意:由于每个人都在继续投票这个问题,我想我会更容易阅读.
Slo*_*ner 21
这样的事情应该有效.你可能需要搞乱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()
Run Code Online (Sandbox Code Playgroud)