This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Get The Gene Name Interactively In A R Plot

I have created a volcano plot to overview the differential expressed genes in microarray data using R.

I am wondering is there a convenient way to identify which gene does the dot represents without go to the raw data,

for example, hover the mouse on a dot and the gene name will be shown.

Many thanks in advance,

r plot visualization gene-expression

Hi, thanks for the link, it seems very interesting !

if using ggplot, check geom_text!!

I use plot at this moment, but definiately will take a look at geom_text when use ggplot.

3 answers

Here is an overview of some alternatives: http://www.statmethods.net/advgraphs/interactive.html

See also the Imagemap package .

The easiest way to get started is possibly using the identify function in R directly, it is not as interactive as one might wish, but it serves its purpose if there are not too many points:

mydata = matrix(rnorm(100), ncol=2, dimnames=list(rows=paste('gene', 1:50), cols=c("x","y")))
plot(mydata)
identify(x=mydata[,1], y=mydata[,2], label=rownames(mydata))

Now, click on points and the label will be added as text to the plot. Right-clicking ends it. Tip: If you click to the right of a point, the label will appear on the right, if clicking below, below, etc. However, for a large number of points this might become very clumsy, then using locator as described below might be a better option.

If you want to work with ranged selection, you can use the locator() function, which returns raw coordinates of your clicks and use function matchpt {Biobase} to find the points closest to your clicks.

plot(mydata)
clicks = locator()
matchpt(cbind(clicks$x, clicks$y), mydata)
  index   distance
 1    38 0.10874959
 2    44 0.13085363
 3     4 0.16094422
 4    48 0.06573644
 5    10 0.07867303

This might look low-tech, but in contrast to more interactive methods works out of the box, while anything else has additional dependencies.

The identify will be more than enough :-) big thanks!

The R Plot.ly library is now available for R and makes it very easy to create an interactive plot like this. I have just recently made a post showing how to create your volcano plot using it. With my example, the gene name will be visible when you mouse over a point on the plot, just as you have requested. See this post here: Creating Interactive Volcano Plots with R and Plot.ly

This is also super easy if you are already good with ggplot as plotly has added a library/function to do just this.

All you need to do is make sure that your 'text' aesthetic is the text of interest.

library(ggplot2)
library(plotly)

## given test.df your data.frame with interesting values to be plotted
head(test.df)
  gene       logFC         pval
1  abo  0.22934272 9.899770e-04
2 ade2 -0.35035923 5.206969e-05
3 ade3 -0.66402834 4.417076e-12
4  aop -0.08875994 1.603035e-01
5  aub -0.26260633 1.136509e-06
6 BicD  0.22848615 5.822924e-06
  
## create ggplot plot
p <- ggplot(test.df,aes(x=-log10(pval),y=logFC,text=gene)) + geom_point()

## visualize plot with plotly
ggplotly(p)

This should cause an html page to pop out with a plot of logFC vs. pval and the ability to mouse over points and display the gene corresponding to said values.

Log in to answer this question.