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

In this case, everything is in order:

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

But using qplot, there are some problems:

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

Does anyone know a similar command or another way to mark specific points in ggplot2 graphs?

+7
source share
3 answers

Here is a method that only works with grid and ggplot2 :

 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 to use a text label instead of a number, you could replace w in a grid.text call with something like letters[w] (or any other vector of labels you need).

If you are going to do several of these, you can wrap this in a function using the last few lines, possibly in a loop. You can also add additional logic to alert you if you don’t click next to a point (for example, an identifier) ​​or move the label closer or further from the point (this version puts a label for the nearest destination at the point you click).

+6
source

You can convert your plot made with ggplot2 to an interactive chart using the ggplotly function from the plotly package, for example:

 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 the cursor over the point of interest and find information about it.

+7
source

I created a little work to use the authentication function inside 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 puts the labels in the same positions as in the chart function ...

hope this helps ...

it would be useful to have more positions then only 4 ... But I do not know how to rewrite the identifier ... for now; -)

+2
source

All Articles