12

I'm trying to use ggplot2 to create and label a scatterplot. 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). 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

3 Answers 3

17

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)
Sign up to request clarification or add additional context in comments.

Comments

4

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)

Comments

1

Subsetting outside of the ggplot function:

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(a, b, data = x, label = lab, geom = c('point','text'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.