[R] extend data frame for plotting heat map in ggplot2

Hadley Wickham hadley at rice.edu
Wed Apr 11 22:28:08 CEST 2012


On Sun, Apr 1, 2012 at 9:16 AM, Till Bayer <till.bayer at kaust.edu.sa> wrote:
> Hi all!
>
> I want to generate a heat map from an all-vs-all comparison. I have the
> data, already scaled to 0-1. However, I have the values only for the
> comparisons in one way, and not for the comparisons between the same group
> (which are always 1), i.e. I have half the matrix and am missing the other
> half and the diagonal.
> What is a good way to get it into a form that ggplot2 can use for the heat
> map?
>
> This is an example of the data I have:
>
> A       B       value
> T1      T2      0.347
> T1      T3      0.669
> T2      T3      0.214
>
> I assume the following is what I need for ggplot (or maybe I don't, if
> ggplot can somehow generate it?):
>
> A       B       value
> T1      T2      0.347
> T1      T3      0.669
> T2      T3      0.214
> T2      T1      0.347
> T3      T1      0.669
> T3      T2      0.214
> T1      T1      1
> T2      T2      1
> T3      T3      1

You can usually do something like:

df <- data.frame(A = 1:2, B = 3:4, value = runif(2))
all <- expand.grid(unique(df[c("A", "B")]))

merge(all, df, all = T)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list