2014-09-06 36 views
3

多邊形邊界重疊ggplot分組磚我有BLOCKNR和貓x和y位置和兩個因素列的數據幀:與取決於額外因素

dput(testData) 
structure(list(xpos = c(2L, 8L, 5L, 8L, 1L, 4L, 5L, 1L, 8L, 4L, 
3L, 2L, 6L, 5L, 1L, 7L, 3L, 4L, 3L, 7L, 1L, 6L, 7L, 7L, 2L, 5L, 
3L, 4L, 6L, 7L, 1L, 5L, 1L, 6L, 4L, 5L, 3L, 6L, 4L, 8L, 1L, 3L, 
4L, 6L, 7L, 3L, 2L, 6L, 4L, 2L, 1L, 7L, 4L, 8L, 2L, 3L, 2L, 5L, 
8L, 2L, 8L, 3L, 3L, 5L, 6L, 7L, 1L, 5L, 6L, 4L, 2L, 6L, 7L, 1L, 
5L, 7L, 2L), ypos = c(1L, 2L, 8L, 1L, 6L, 7L, 1L, 4L, 6L, 1L, 
2L, 3L, 4L, 5L, 7L, 8L, 10L, 2L, 6L, 9L, 1L, 2L, 10L, 4L, 5L, 
6L, 3L, 5L, 9L, 3L, 9L, 10L, 3L, 7L, 8L, 2L, 5L, 6L, 3L, 4L, 
10L, 1L, 4L, 10L, 2L, 8L, 9L, 3L, 6L, 8L, 5L, 7L, 10L, 3L, 4L, 
7L, 2L, 4L, 5L, 6L, 7L, 9L, 4L, 7L, 8L, 1L, 2L, 9L, 5L, 9L, 10L, 
1L, 6L, 8L, 3L, 5L, 7L), blocknr = c(1L, 3L, 2L, 3L, 1L, 2L, 
2L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 1L, 3L, 1L, 2L, 
3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 2L, 2L, 1L, 3L, 
2L, 3L, 1L, 1L, 2L, 3L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 
1L, 2L, 1L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 2L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L), cat = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", "B", "C" 
), class = "factor")), .Names = c("xpos", "ypos", "blocknr", 
"cat"), row.names = c(NA, -77L), class = "data.frame") 

我做了以下ggplot代碼,使2D概述:

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)+ 
coord_cartesian(ylim = c(0.5, ymax + 0.5)) + 
coord_cartesian(xlim = c(0.5, xmax + 0.5)) + 
scale_x_continuous(breaks=seq(1,xmax,1))+ 
scale_y_continuous(breaks=seq(1,ymax,1))+ 
#geom_polygon(aes(group=blocknr))+ 
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()) 

產生以下結果: enter image description here

現在我想通過突出顯示每個組blocknrs的畫他們周圍的邊框,如下圖所示: enter image description here

我打得四處geom_polygongeom_path,但我不能完全找到一個方法來做到這一點。有沒有一種通用的方法可以在ggplot中實現這一點,而無需構建算法來計算每條線的位置,並將這些線添加爲geom_segment

回答

5

據我所知,沒有辦法用標準的ggplot2 tile選項來做到這一點。但是把它們作爲細分來構建它們並不麻煩。例如

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

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

在這裏,我們基本上把所有的行/列分配數據並創建一個矩陣,基本上看起來像的情節,但我們將與塊編號。現在,我們將通過查找塊數的變化來掃描我們需要添加「牆」的位置,因爲我們分別遍歷每行和每列。

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

而且您可以添加對geom_segments的調用以將水平和垂直牆添加到圖。

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

這給

enter image description here

+0

美麗和優雅的解決方法/解決方案!謝謝! – svdwoude 2014-09-07 08:25:21