Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 Scatter Plot Labels

Tags:

r

ggplot2

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

like image 482
Brandon Bertelsen Avatar asked Nov 29 '09 20:11

Brandon Bertelsen


2 Answers

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)
like image 75
hadley Avatar answered Sep 28 '22 10:09

hadley


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)
like image 39
rcs Avatar answered Sep 28 '22 12:09

rcs