Issue
I have a xyz matrix stored in a .csv file that, for example, looks like the following:
1 2 3 4
1 1 1 -1 1
2 -1 1 1 1
3 1 -1 1 -1
4 1 1 -1 1
where the rows are x, columns are y and entries are z. I could store this matrix in a dataframe.
I want to create a heatmap of this matrix, where the entries would just be two colors, depending on 1 or -1. The plot would look like the following for an 18 X 18 matrix (done by a colleague in matlab probably):
where, say, yellow is for 1 and blue for -1.
How can I do this in R?
Solution
Use geom_tile(). First you need to flatten your dataframe using melt from the package reshape2.
library(reshape2)
flat_df <- melt(t(df), value.name = "Value")
ggplot(data=flat_df,aes(y=y,x=x))+geom_tile(aes(fill=Value))+scale_y_reverse()
Answered By - Soccerama Answer Checked By - David Marino (PHPFixing Volunteer)

0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.