1

I have a CSV file that's a grid.

Link to the raw csv file on github

I want to plot this using ggplot so that it will look like this plot.

enter image description here

Where the colours are representing the values in the cells. (The provided csv has other values)

I can't get it to work without using a x and y aesthetic. Maybe it can be fixed with using rownames and colnames, but now the first row and column are used as colnames and rownames. Need to find something to change that as well.

I feel like I'm going nuts. Want to use geom_bin2d() or even stat_density_2d() but couldn't get is to work.

Lucius
  • 132
  • 9
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Include sample data in the question itself rather than linking to an external website and show the code you tried. If your data is already binned, then use something like `geom_tile()` instead. – MrFlick Mar 24 '21 at 20:32

1 Answers1

1

Maybe you can try to melt it and then geom_raster():

library(ggplot2)
library(reshape2)

x = read.csv("https://raw.githubusercontent.com/Friends-of-Tracking-Data-FoTD/LaurieOnTracking/master/EPV_grid.csv",header=FALSE)

ggplot(melt(as.matrix(t(x))), aes(Var1,Var2, fill=value)) + 
geom_raster() + 
scale_fill_viridis_c(direction=-1) + 
theme_minimal()

enter image description here

Or:

ggplot(melt(as.matrix(t(x))), aes(Var1,Var2, fill=value)) + 
geom_tile() + 
scale_fill_viridis_c(direction=-1) + 
theme_minimal()

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72