rggplot2geom-raster

Using geom_raster function returns invalid 'type' (list) of argument


I am trying to plot a matrix data into d*d grid using R. So I used geom_raster function.

I have data with three variables: row and col specify the location for each data point, and w is the data I wish to plot using geom_raster.

I simulate the three variables below:

row <- rep(1:55, 55)
col <- rep(1:55, 55)
w <- runif(55*55)

I order to use ggplot, I convert the data into dataframe form:

df <- data.frame(
   row = row, col = col, w = w
)

Now I use df to generate plot

ggplot(data = df, aes(row, col)) + geom_raster(fill = aes(w))

But it returns an error that says

Error in stats::complete.cases(df[, vars, drop = FALSE]) : invalid 'type' (list) of argument

I end up do not know how to fix this bug, would anyone help me out?


Solution

  • The syntax for your geom_raster doesn't look right.

    Try this instead:...

        ggplot(data = df, aes(row, col)) + geom_raster(aes(fill=w))