首页 > 解决方案 > 在不使用 ggplot 的情况下标记图 R 中的特定数据点

问题描述

我正在尝试标记图中阴影的数据点。在此处输入图像描述

这是我的示例数据:

    genes     logFC       PValue
1 Arhgap8 -5.492152 2.479473e-99
2    Asns -2.519970 2.731718e-93
3    Bmp4 -1.663583 4.767201e-72
4   Casp1 -1.650139 2.212689e-25
5    Ctgf -1.272772 1.000103e-61
6    Eya4 -2.328052 2.077364e-68

到目前为止我的情节代码:

plot(sample$logFC,-log10(as.numeric(sample$PValue)),pch = 20,xlab = 'Log2 FoldChange',ylab = '-Log10 p-value',col = 'blue',xlim = c(-10,8),ylim = c(0,300),cex.lab=1.5, cex.axis=1.5)
points(sample$logFC,-log10(as.numeric(sample$PValue)),col = "dark green")
with(subset(sample,genes=='Arhgap8'),points(logFC,-log10(as.numeric(PValue)),pch = 20, col="orange"))

我试过使用下面的命令,包括文本;但它没有显示标签。

with(subset(sample,genes=='Arhgap8'),points(logFC,-log10(as.numeric(PValue)),pch = 20, col="violet"),text(sample,labels = sample$genes,cex = 0.9,pos = 4))

标签: rplotscatter-plot

解决方案


with用于运行命令的正确方法是

with(subset(sample, genes=='Arhgap8'), { 
  points(logFC, -log10(as.numeric(PValue)), pch = 20, col="violet")
  text(logFC, -log10(as.numeric(PValue)), labels = genes, cex = 0.9, pos = 4)
})

当您使用 传递更多参数时with(),它们会被默默地忽略。例如

with(iris, mean(Sepal.Length), stop("not run"))

推荐阅读