简体   繁体   English

ggplot2 Scatter Plot Labels

[英]ggplot2 Scatter Plot Labels

I'm trying to use ggplot2 to create and label a scatterplot. 我正在尝试使用ggplot2来创建和标记散点图。 The variables that I am plotting are both scaled such that the horizontal and the vertical axis are plotted in units of standard deviation (1,2,3,4,...ect from the mean). 我正在绘制的变量都被缩放,使得水平轴和垂直轴以标准偏差(1,2,3,4,...平均值)为单位绘制。 What I would like to be able to do is label ONLY those elements that are beyond a certain limit of standard deviations from the mean. 我希望能够做的只是标记那些超出平均值标准偏差的元素。 Ideally, this labeling would be based off of another column of data. 理想情况下,此标签将基于另一列数据。

Is there a way to do this? 有没有办法做到这一点?

I've looked through the online manual, but I haven't been able to find anything about defining labels for plotted data. 我查看了在线手册,但是我无法找到有关为绘图数据定义标签的任何信息。

Help is appreciated! 感谢帮助!

Thanks! 谢谢!

BEB BEB

Use subsetting: 使用子集:

library(ggplot2)
x <- data.frame(a=1:10, b=rnorm(10))
x$lab <- letters[1:10]
ggplot(data=x, aes(a, b, label=lab)) + 
  geom_point() + 
  geom_text(data = subset(x, abs(b) > 0.2), vjust=0)

The labeling can be done in the following way: 标签可以通过以下方式完成:

library("ggplot2")
x <- data.frame(a=1:10, b=rnorm(10))
x$lab <- rep("", 10)   # create empty labels
x$lab[c(1,3,4,5)] <- LETTERS[1:4]   # some labels
ggplot(data=x, aes(x=a, y=b, label=lab)) + geom_point() + geom_text(vjust=0)

Subsetting outside of the ggplot function: 在ggplot函数之外的子集:

library(ggplot2)
set.seed(1)
x <- data.frame(a = 1:10, b = rnorm(10))
x$lab <- letters[1:10]
x$lab[!(abs(x$b) > 0.5)] <- NA
ggplot(data = x, aes(a, b, label = lab)) + 
  geom_point() + 
  geom_text(vjust = 0) 

Using qplot: 使用qplot:

qplot(a, b, data = x, label = lab, geom = c('point','text'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM