Draw a plot for a matrix in r

1.3k views Asked by At

I have a matrix with three variables Row = Time, column = Date and the third variable Money which its value is an intersection of rows and columns. e.g. For Time = 5 and Date = 10, Money is 12 and for Time = 6 and Date = 15, Money is 15. I would like to draw the value of Money for the intersection of x_axis = Time and Y_axis = Date.

How to place Money in below?

plot.new()  
matplot(Time,Date, type = "p", lty = 1:5, lwd = 1, lend = par("lend"),col = 1,
        pch = 17 , xlab = "Time", ylab = "Date", xlim = range(0,40), ylim = range (0,120))  
1

There are 1 answers

3
Mario On

I think you could use geom_raster if you convert your data to a data.frame first:

ggplot(data, aes(Time, Date)) +
 geom_raster(aes(fill = Money))

See more on this here: http://docs.ggplot2.org/current/geom_tile.html


edit:

see with random data here:

time <- c(1:100)
date <- c(1:100)
data <- expand.grid(TIME = time, DATE = date)

data$MONEY <- runif(1:10000, 0, 10)

ggplot(data, aes(TIME, DATE)) +
  geom_raster(aes(fill = MONEY), interpolate = F)

enter image description here