Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-faceted heat map with ggplot for selected portion of X with additional text labels on it

Tags:

r

ggplot2

heatmap

I have the following data:

Id = paste ("ID-", 1:5, sep = "")
position <- rep(seq (1, 100,10), each = 5)
group = rep (rep(rep (1:5, each = length (Id)), each = length(position)))
yvar <- rnorm (length(position), 0.5, 0.1)
ycat <- c(sample (c("A", "B"), length(yvar), replace = TRUE))
namevar <- rep(Id, length(group)/length(Id))
mydf <- data.frame (namevar, group, position, yvar, ycat)

group is a faceting variable, position is a continous x variable. yvar is used for filling the color of the tiles. ycat is a text label for each tile. I want to create a plot with empty space for all values, except certain tiles that I select to plot with a fill color and labels.

Here is what I have so far:

  ggplot(mydf,aes(y=Id,x=position)) +
      facet_wrap(~group) +     
      geom_tile(aes(fill = yvar),colour = "black") +
      geom_text(aes(label = ycat)) +
      labs(x = NULL,y = NULL)

enter image description here

I'd like the plot to look like this except have blank space everywhere except, for instance, group 1 between 30-50 and group 5 between 20-60, sort of like this:

enter image description here

like image 266
SHRram Avatar asked May 04 '12 12:05

SHRram


1 Answers

This will produce your last plot, but only shade selected regions:

ggplot(mydf,aes(y=Id,x=position)) +
  facet_wrap(~group) + 
  geom_blank() +    
  geom_tile(data = subset(mydf,(group == 1 & position >= 30 & position <= 50) | 
                                (group == 5 & position >= 20 & position <= 60)),aes(fill = yvar),colour = "black") +
  geom_text(data = subset(mydf,(group == 1 & position >= 30 & position <= 50) | 
                                (group == 5 & position >= 20 & position <= 60)),aes(label = ycat),size = 3) +
  labs(x = NULL,y = NULL)
like image 63
joran Avatar answered Sep 30 '22 13:09

joran