Davi Moreira
Davi Moreira

Reputation: 953

Is there any way to use the Identify command with ggplot 2?

In this case, everything is ok:

x <- 1:10 
y <- x^3
plot(x, y)
identify(x, y)

But, using qplot there are some troubles:

x <- 1:10 
y <- x^3
qplot(x, y)
identify(x, y)

Does anybody know a similar command or other way to label specific points in ggplot2 graphs?

Upvotes: 10

Views: 11727

Answers (3)

GegznaV
GegznaV

Reputation: 5590

You may convert your plot, made with ggplot2 to interactive graph by using function ggplotly from package plotly, e.g.:

library(ggplot2)
library(plotly)

# Prepare data
x <- 1:10  
y <- x^3 
names <- paste("Point", LETTERS[x])

# Make a plot with `ggplot` as usual
qplot(x, y, label = names) 

# Convert it to interactive plot
ggplotly()

Then move your cursor over a point of interest and find information about it.

Upvotes: 15

drmariod
drmariod

Reputation: 11762

I created a little work around to use the identify function within ggplot

df <- data.frame(x=c(1.8,2.1,3.1,2.8,3.1,4.9,5.1,3.2,2.2),
    y=c(3.2,2.3,4.1,5.2,3.1,2,1.9,2.1,3),
    name=c('agw452','hhewhdsgwgb','cgahawrhs','gsarhrwhd','ehhrwhrwwrw','fhhrwwrw','ghhWwr','hhHRWRHwr','ihwhrHWRHw'))
plot(df$x,df$y)
identified <- identify(df$x,df$y,labels=df$name,pos=T)
df$pos <- NA
df[identified$ind,]$pos <- identified$pos
ggplot(df,aes(x=x,y=y)) + geom_point() + 
    geom_point(data=subset(df,!is.na(pos)),aes(color='red')) +
    geom_text(data=subset(df,pos == 1),aes(label=name),vjust=1) + 
    geom_text(data=subset(df,pos == 2),aes(label=name),hjust=1) +
    geom_text(data=subset(df,pos == 3),aes(label=name),vjust=-.5) + 
    geom_text(data=subset(df,pos == 4),aes(label=name),hjust=0)

It uses the index and positions of your clicks and places the labels at the same positions as in the plot function...

hope that helps...

would be useful to have more positions then just 4... But don't know how to rewrite identify... yet ;-)

Upvotes: 3

Greg Snow
Greg Snow

Reputation: 49660

Here is a method that works using only the grid and ggplot2 packages:

library(ggplot2)
library(grid)

x <- 1:10  
y <- x^3 
qplot(x, y) 

downViewport('panel-3-4')
pushViewport(dataViewport(x,y))

tmp <- grid.locator('in')
tmp.n <- as.numeric(tmp)
tmp2.x <- as.numeric(convertX( unit(x,'native'), 'in' ))
tmp2.y <- as.numeric(convertY( unit(y,'native'), 'in' ))

w <- which.min( (tmp2.x-tmp.n[1])^2 + (tmp2.y-tmp.n[2])^2 )
grid.text(w, tmp$x, tmp$y )

If you want a text label instead of the number your could replace w in the call to grid.text with something like letters[w] (or whatever vector of labels you want).

If you are going to be doing several of these then you could wrap this in a function with the last few lines possibly in a loop. You could also add addtional logic to warn if you don't click near a point (like identify does) or to move the label closer or further from the point (this version places the label for the nearest datapoint at the point that you click).

Upvotes: 8

Related Questions