Overlay ggplot grouped tiles with polygon border depending on extra factor

喜你入骨 提交于 2019-12-05 23:15:29
MrFlick

As far as I know, there is no way to do this with standard ggplot2 tile options. But it's not to much trouble to constuct them if you do it as segments. For example

ymax <- max(testData$ypos)
xmax <- max(testData$xpos)

m <- matrix(0, nrow=ymax, ncol=xmax)
m[as.matrix(testData[,2:1])] <- testData[,3]

Here we are basically taking all the row/col assignment data and creating a matrix that essentially looks like the plot but we will with the block numbers. Now, we will scan for the locations we need to add "wall" by looking for changes in the block numbers as we go across each row and column separately.

has.breaks<-function(x) ncol(x)==2 & nrow(x)>0

hw<-do.call(rbind.data.frame, Filter(has.breaks, Map(function(i,x) 
    cbind(y=i,x=which(diff(c(0,x,0))!=0)), 1:nrow(m), split(m, 1:nrow(m)))))
vw<-do.call(rbind.data.frame, Filter(has.breaks, Map(function(i,x)
    cbind(x=i,y=which(diff(c(0,x,0))!=0)), 1:ncol(m), as.data.frame(m))))

And you can add calls to geom_segments to add the horizontal and vertical walls to the plot.

ggplot(data=testData, aes(x=xpos,y=ypos))+
    geom_tile(aes(fill=cat), colour = "white")+
    scale_fill_manual(values = c('A' = '#F8766D','C' = '#8ABF54','B' = '#C1DDA5'))+
    geom_text(aes(x=xpos,y=ypos,label=blocknr),size=3)+
    geom_segment(data=hw, aes(x=x-.5, xend=x-.5, y=y-.5, yend=y+.5))+
    geom_segment(data=vw, aes(x=x-.5, xend=x+.5, y=y-.5, yend=y-.5))+
    coord_cartesian(ylim = c(0.4, ymax + 0.6)) +
    coord_cartesian(xlim = c(0.4, xmax + 0.6)) +
    scale_x_continuous(breaks=seq(1,xmax,1))+
    scale_y_continuous(breaks=seq(1,ymax,1))+
    theme(axis.line = element_line(colour = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())

which gives

The desplot package will do this for you (using lattice):

library(desplot)
desplot(cat ~ xpos*ypos, testData, out1=blocknr, text=blocknr, main="testData")

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