在R中使用facet_wrap

时间:2018-03-23 21:34:58

标签: r ggplot2 facet-wrap

我在this answer中尝试使用方法在我的热图的“区域”之间包含白线。这是我使用ChickWeight数据集时遇到的同样问题。

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() 

这是我原来的情节,我想要垂直线来更好地区分x轴上的事物组(在这种情况下,鸡) 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() 

这就是我得到的。我想要将被压扁的地块拉伸整个长度。我究竟做错了什么? enter image description here

1 个答案:

答案 0 :(得分:3)

我将Chick变为numeric变量并允许x标度免费。

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