Multifaceted heat map with ggplot for selected part X with additional text labels on it

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 faceted variable, position is a continuous variable x. yvar used to fill the color of tiles. ycat is the text label for each fragment. I want to create a plot with empty space for all values ​​except certain fragments that I choose to build with fill color and labels.

Here is what I still have:

  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 would like this plot to look like it is an empty place all over the world, with the exception, for example, of group 1 between 30-50 and group 5 between 20-60, like:

enter image description here

+6
source share
1 answer

This will lead to the creation of your last plot, but will only display selected areas:

 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) 
+2
source

Source: https://habr.com/ru/post/914900/


All Articles