elisa
elisa

Reputation: 995

Use facet_wrap in R

I tried the approach in this answer to use facets to include white lines between "zones" of my heatmap. Here's the same problem I have using the ChickWeight dataset.

library(reshape)
library(ggplot2)
ChickWeight.long <- melt(ChickWeight, id = c("Chick", "Diet"))

#Non-split heatmap
ggplot(ChickWeight.long, aes(x = Chick, y = variable, fill=value)) +
  geom_tile(colour="white",size=0.1) + #add grid lines between the tiles
  xlab("Chick") + ylab("") + labs(fill = "weight") +  
  theme_minimal() 

This is my original plot, I'd like vertical lines to better distinguish groups of things (in this case, chickens) in the x axis Heatmap without breaks

#Attempt of split heatmap
ggplot(ChickWeight.long, aes(x = Chick, y = variable, fill=value)) +
  facet_wrap(~Diet) +
  geom_tile(colour="white",size=0.1) + #add grid lines between the tiles
  xlab("Chick") + ylab("") + labs(fill = "weight") +  
  theme_minimal() 

This is what I get. I'd like the squished plots to be stretched the whole length. What am I doing wrong? enter image description here

Upvotes: 1

Views: 2515

Answers (1)

smanski
smanski

Reputation: 541

I made Chick a numeric variable and allowed the x scale to be free.

ChickWeight.long$Chick <- as.numeric(as.character(ChickWeight.long$Chick))

ggplot(ChickWeight.long, aes(x = Chick, y = variable, fill=value)) +
  facet_wrap(~Diet, scale = "free_x") +
  geom_tile(colour="white",size=0.1) + #add grid lines between the tiles
  xlab("Chick") + ylab("") + labs(fill = "weight") +  
  theme_minimal() 

enter image description here

Upvotes: 3

Related Questions