White space between tiles in heatplot ggplot

时光怂恿深爱的人放手 提交于 2019-12-04 13:26:27

Option 1

Use facets

mockdata$type = ifelse(mockdata$variable %in% LETTERS[1:3], "1", "2")

ggplot(mockdata) + 
  facet_wrap(~ type, scales='free_x') +
  geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red")  + 
  scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") + 
  theme_minimal() + 
  theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) + 
  labs(title="", x="", y="", fill="") 

Although this is not quite perfect, because dropping the unused x-axis categories from each facet requires specifying scales='free_x' - which unfortunately does not keep the boxes the same width in each facet.

Option2

To remedy this we can also set space="free_x" in facet_grid. Which I think gives what you are looking for:

ggplot(mockdata) + 
  facet_grid(~ type, scales='free_x', space="free_x") +
  geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red")  + 
  scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") + 
  theme_minimal() + 
  theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) + 
  labs(title="", x="", y="", fill="") 

Option 3

An alternative solution is to create separate plots for each category, and arrange them together. Here I put them together using cowplot::plot_grid, which allows us to set the relative widths how we want them (although you may need to tweak the rel_widths values a bit to get it just right):

library(cowplot)
p1 <- ggplot(mockdata[which(mockdata$type=="1"),]) + 
  geom_tile(aes(variable, Measurement, fill = plotval), colour = "dark red")  + 
  scale_fill_gradient2(limits=c(-20, 20), high = "firebrick3", low = "dodgerblue4") + 
  theme_minimal() + 
  theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28),
        legend.position="none") + 
  labs(title="", x="", y="", fill="") 

p2 <- ggplot(mockdata[which(mockdata$type=="2"),]) + 
  geom_tile(aes(variable, Measurement, fill = plotval), colour = "dark red")  + 
  scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") + 
  theme_minimal() + 
  theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_blank()) + 
  labs(title="", x="", y="", fill="") 

plot_grid(p1,p2, nrow = 1,  rel_widths = c(1,2))

Option 4

You can create an extra factor level in the data, which lies between the columns you want to separate, and then plot tiles for that column in white.

mockdata = rbind(mockdata, 
                 data.frame(Measurement=1:20, 
                            variable="", 
                            Pval = NA,
                            effect = NA,
                            direction = NA,
                            plotval = 0,
                            category = "Measured"))
mockdata$variable = factor(mockdata$variable, levels = c("A", "B", "C", "", "a", "b", "c", "d", "e","f"))

ggplot(mockdata, aes(variable, Measurement)) + 
  geom_tile(fill = NA, colour = NA)  + 
  geom_tile(data = mockdata[which(mockdata$variable==""),], fill = "white", colour = "white")  + 
  geom_tile(data = mockdata[which(mockdata$variable!=""),], aes(fill = plotval), colour = "dark red")  + 
  scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") + 
  theme_minimal() + 
  theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) + 
  labs(title="", x="", y="", fill="") 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!