El Dude
El Dude

Reputation: 5618

R click position for matrix plot

My algorithm calculates a symmetric matrix m. What I need to do is to create a plot similar to image for instance and make this plot clickable, so that a click callback would print out the positions indeces for the clicked matrix entry. How can I do this with R?

m = createMatrix(data);  
image(1:matrixSize,1:matrixSize,m);

Upvotes: 1

Views: 62

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24262

Try this solution based on the plotly package.
Hope it can help you.

# A symmetrix matrix
mtx <- cor(matrix(rnorm(1000),ncol=5))

# From wide to long format
library(reshape)
mtx.long <- melt(mtx)

# Create an image plot of the matrix 
library(ggplot2)
p <- ggplot(data=mtx.long, aes(x=X1, y=X2, fill=value))+geom_raster()

# Create an interactive plot
library(plotly)
ggplotly(p)

enter image description here

Upvotes: 1

Related Questions