Skip to content
Prev 317686 / 398502 Next

Plot a Matrix as an Image with ggplot

Hi:

See if the following works for you:

library(reshape2)
library(ggplot2)
tdm <- melt(testData)

ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) +
    labs(x = "MHz", y = "Threshold", fill = "Value") +
    geom_raster() +
    scale_fill_manual(breaks = levels(factor(tdm$value)),
                      values = c("white", "black")) +
    theme(plot.background = element_rect(fill = "grey90"),
          legend.background = element_rect(fill = "grey90")) +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0))

It was necessary to create a different plot background color because
you wanted to remove the padding, which blended much of the plot
boundary with the original white background, and had to do something
similar to the legend background so that you could see the white
legend box.

The expand = c(0, 0) argument to the two scale functions answers your
second question, and converting value from numeric to vector answers
the first.

Dennis
On Fri, Feb 15, 2013 at 3:24 PM, Alaios <alaios at yahoo.com> wrote: