根据ggplot中的因子在区域之间绘制线条

时间:2015-08-24 07:28:49

标签: r ggplot2

我正在使用geom_tilefacet_grid来绘制一些在ptype列上计算的数据。 MWE如下:

library(ggplot2)


data<-"T So Sigma Gamma Rtot Sof Sff ptype
8078 10 3 0 0.6 3 3 3 factor1
8089 10 3 0.1 0.6 3 3 3 factor1
8100 10 3 0.2 0.6 3 3 3 factor1
8111 10 3 0.3 0.6 3 3 3 factor1
8122 10 3 0.4 0.6 3 3 3 factor1
8133 10 3 0.5 0.6 3 3 3 factor1
8144 10 3 0.6 0.6 3 3 3 factor1
8155 10 3 0.7 0.6 3 3 3 factor1
8166 10 3 0.8 0.6 3 3 3 factor1
8177 10 3 0.9 0.6 3 3 3 factor1
8188 10 3 1 0.6 3 3 3 factor1
49304 20 3 0 0.6 3 3 3 factor1
49315 20 3 0.1 0.6 3 3 3 factor1
49326 20 3 0.2 0.6 3 3 3 factor1
49337 20 3 0.3 0.6 3 3 3 factor1
49348 20 3 0.4 0.6 3 3 3 factor1
49359 20 3 0.5 0.6 3 3 3 factor1
49370 20 3 0.6 0.6 3 3 3 factor1
49381 20 3 0.7 0.6 3 3 3 factor1
49392 20 3 0.8 0.6 3 3 3 factor1
49403 20 3 0.9 0.6 3 3 3 factor1
49414 20 3 1 0.6 3 3 3 factor1
76198 30 3 0 0.6 3.76171 3 3.76171 factor2
76209 30 3 0.1 0.6 3.76171 3 3.76171 factor2
76220 30 3 0.2 0.6 3.76171 3 3.76171 factor2
76231 30 3 0.3 0.6 3.76171 3 3.76171 factor2
76242 30 3 0.4 0.6 3.76171 3 3.76171 factor2
76253 30 3 0.5 0.6 3.76171 3 3.76171 factor2
76264 30 3 0.6 0.6 3.76171 3 3.76171 factor2
76275 30 3 0.7 0.6 3.76171 3 3.76171 factor2
76286 30 3 0.8 0.6 3.76171 3 3.76171 factor2
76297 30 3 0.9 0.6 3.84754 0.1 0.16195 factor3
76309 30 3 1 0.6 3.84754 0.1 0.16195 factor3
127588 40 3 0 0.6 5.35608 3 5.35608 factor2
127599 40 3 0.1 0.6 5.35608 3 5.35608 factor2
127610 40 3 0.2 0.6 5.35608 3 5.35608 factor2
127623 40 3 0.3 0.6 5.35608 3 5.35608 factor2
127634 40 3 0.4 0.6 5.35825 0 0 factor3
127645 40 3 0.5 0.6 5.35825 0 0 factor3
127656 40 3 0.6 0.6 5.47198 0.2 0.51703 factor3
127669 40 3 0.7 0.6 5.56348 0.2 0.51703 factor3
127680 40 3 0.8 0.6 5.73882 0.8 1.85069 factor3
127691 40 3 0.9 0.6 6.03841 0.9 2.05019 factor3
127702 40 3 1 0.6 6.37118 1.6 3.30385 factor3
162297 50 3 0 0.6 7.36898 3 7.36898 factor2
162309 50 3 0.1 0.6 7.36898 3 7.36898 factor2
162321 50 3 0.2 0.6 7.36898 3 7.36898 factor2
162333 50 3 0.3 0.6 7.49796 0.1 0.42697 factor3
162344 50 3 0.4 0.6 7.64096 0.1 0.42697 factor3
162357 50 3 0.5 0.6 7.78737 0.4 1.54882 factor3
162371 50 3 0.6 0.6 8.358 0.8 2.78492 factor3
162389 50 3 0.7 0.6 8.92932 1.1 3.59043 factor3
162404 50 3 0.8 0.6 9.58623 1.7 4.97184 factor3
162416 50 3 0.9 0.6 10.2468 2.1 5.77926 factor3
162448 50 3 1 0.6 10.8984 2.9 7.20516 factor3
195012 80 3 0 0.6 15.9047 3 15.9047 factor2
195028 80 3 0.1 0.6 17.0201 0.1 1.68242 factor3
195058 80 3 0.2 0.6 18.8161 0.4 5.00423 factor3
195086 80 3 0.3 0.6 21.2521 0.8 7.83781 factor3
195128 80 3 0.4 0.6 23.3847 1.4 10.8074 factor3
195175 80 3 0.5 0.6 25.4261 2 13.0496 factor3
195304 80 3 0.6 0.6 27.1939 2.6 14.8517 factor3
195342 80 3 0.7 0.6 28.8538 3.4 16.8773 factor3
195373 80 3 0.8 0.6 30.3827 3.8 17.7612 factor3
195418 80 3 0.9 0.6 31.8626 4.8 19.7527 factor3
195513 80 3 1 0.6 33.201 5.7 21.341 factor3
231307 60 3 0 0.6 9.80308 3 9.80308 factor2
231319 60 3 0.1 0.6 9.80308 3 9.80308 factor2
231330 60 3 0.2 0.6 10.0912 0.1 0.68484 factor3
231343 60 3 0.3 0.6 10.4219 0.3 1.85263 factor3
231354 60 3 0.4 0.6 11.1548 0.6 3.2646 factor3
231367 60 3 0.5 0.6 12.1336 1.1 5.1274 factor3
231378 60 3 0.6 0.6 13.1552 1.6 6.62604 factor3
231389 60 3 0.7 0.6 14.1498 1.8 7.15294 factor3
231401 60 3 0.8 0.6 15.1461 2.6 9.00079 factor3
231412 60 3 0.9 0.6 16.1305 3.2 10.1851 factor3
231423 60 3 1 0.6 17.0404 4 11.5972 factor3
263033 90 3 0 0.6 19.5131 3 19.5131 factor2
263050 90 3 0.1 0.6 22.1254 0.2 4.33427 factor3
263066 90 3 0.2 0.6 25.5275 0.6 8.83941 factor3
263079 90 3 0.3 0.6 28.7836 1.1 12.233 factor3
263094 90 3 0.4 0.6 31.494 1.7 15.1418 factor3
263109 90 3 0.5 0.6 33.8865 2.3 17.3951 factor3
263122 90 3 0.6 0.6 35.9751 3.1 19.785 factor3
263140 90 3 0.7 0.6 37.8814 4.1 22.2404 factor3
263156 90 3 0.8 0.6 39.6486 4.6 23.3014 factor3
263171 90 3 0.9 0.6 41.2937 5.5 25.0511 factor3
263187 90 3 1 0.6 42.8274 6.4 26.6386 factor3
278614 70 3 0 0.6 12.6641 3 12.6641 factor2
278634 70 3 0.1 0.6 12.8453 0.1 1.08362 factor3
278649 70 3 0.2 0.6 13.6719 0.2 1.99456 factor3
278682 70 3 0.3 0.6 14.9937 0.5 4.13083 factor3
278741 70 3 0.4 0.6 16.5076 1 6.65247 factor3
278768 70 3 0.5 0.6 18.0436 1.6 8.91378 factor3
278798 70 3 0.6 0.6 19.522 2.1 10.426 factor3
278842 70 3 0.7 0.6 20.8664 2.5 11.4774 factor3
278881 70 3 0.8 0.6 22.1743 3.5 13.7218 factor3
278931 70 3 0.9 0.6 23.3991 4.1 14.8892 factor3
279090 70 3 1 0.6 24.6061 4.9 16.2968 factor3
370330 100 3 0 0.6 23.4521 3 23.4521 factor2
370344 100 3 0.1 0.6 28.6985 0.3 7.83825 factor3
370365 100 3 0.2 0.6 33.7053 0.8 13.3056 factor3
370386 100 3 0.3 0.6 37.4726 1.2 16.0314 factor3
370406 100 3 0.4 0.6 40.6543 1.9 19.5437 factor3
370427 100 3 0.5 0.6 43.3269 2.6 22.1924 factor3
370463 100 3 0.6 0.6 45.7282 3.3 24.3216 factor3
370492 100 3 0.7 0.6 47.8327 4.4 27.0937 factor3
370520 100 3 0.8 0.6 49.7812 5 28.3945 factor3
370557 100 3 0.9 0.6 51.6038 5.7 29.7796 factor3
370602 100 3 1 0.6 53.3879 7.2 32.4554 factor3
412193 110 3 0 0.6 27.6951 3 27.6951 factor2
412204 110 3 0.1 0.6 36.5918 0.4 12.0252 factor3
412215 110 3 0.2 0.6 42.7655 0.9 17.4787 factor3
412226 110 3 0.3 0.6 47.1396 1.6 21.9812 factor3
412237 110 3 0.4 0.6 50.7359 2.2 24.7757 factor3
412248 110 3 0.5 0.6 53.6021 2.9 27.3664 factor3
412259 110 3 0.6 0.6 56.2749 3.7 29.762 factor3
412270 110 3 0.7 0.6 58.5612 4.8 32.4988 factor3
412281 110 3 0.8 0.6 60.7087 5.5 34.0162 factor3
412292 110 3 0.9 0.6 62.673 6.7 36.3154 factor3
412303 110 3 1 0.6 64.5379 7.8 38.2264 factor3
430210 120 3 0 0.6 32.1833 3 32.1833 factor2
430221 120 3 0.1 0.6 45.6131 0.4 15.1777 factor3
430232 120 3 0.2 0.6 52.7576 1 22.0867 factor3
430243 120 3 0.3 0.6 57.6179 1.6 26.0984 factor3
430254 120 3 0.4 0.6 61.516 2.3 29.5158 factor3
430265 120 3 0.5 0.6 64.6629 3.2 32.8588 factor3
430276 120 3 0.6 0.6 67.5344 3.8 34.6713 factor3
430288 120 3 0.7 0.6 69.994 4.5 36.5286 factor3
430299 120 3 0.8 0.6 72.2754 5.8 39.5058 factor3
430310 120 3 0.9 0.6 74.3846 6.6 41.0843 factor3
430321 120 3 1 0.6 76.3764 8.3 44.0881 factor3
466534 130 3 0 0.6 36.9146 3 36.9146 factor2
466545 130 3 0.1 0.6 55.7089 0.5 20.4219 factor3
466556 130 3 0.2 0.6 63.5321 1.1 27.0676 factor3
466567 130 3 0.3 0.6 68.8209 1.9 32.1692 factor3
466578 130 3 0.4 0.6 72.9612 2.6 35.3899 factor3
466589 130 3 0.5 0.6 76.4129 3.4 38.296 factor3
466600 130 3 0.6 0.6 79.3824 4 40.1256 factor3
466611 130 3 0.7 0.6 82.0293 4.9 42.4973 factor3
466622 130 3 0.8 0.6 84.461 6.4 45.847 factor3
466633 130 3 0.9 0.6 86.6854 6.9 46.8273 factor3
466644 130 3 1 0.6 88.7074 8.7 50.0145 factor3
475416 140 3 0 0.6 41.8711 3 41.8711 factor2
475427 140 3 0.1 0.6 66.4409 0.5 24.4193 factor3
475438 140 3 0.2 0.6 75.0445 1.1 31.5349 factor3
475449 140 3 0.3 0.6 80.6704 1.9 36.9064 factor3
475460 140 3 0.4 0.6 85.0171 2.6 40.2685 factor3
475471 140 3 0.5 0.6 88.651 3.5 43.6432 factor3
475482 140 3 0.6 0.6 91.813 4.3 46.0965 factor3
475493 140 3 0.7 0.6 94.6003 5.5 49.1544 factor3
475504 140 3 0.8 0.6 97.1285 6.4 51.1477 factor3
475515 140 3 0.9 0.6 99.521 7.4 53.1594 factor3
475526 140 3 1 0.6 101.678 9 55.9788 factor3"


dsub<-read.table(text=data)
dsub<-data.frame(dsub)
dsub$ptype<-factor(dsub$ptype,levels=c("factor1","factor2","factor3"))

p<- ggplot(dsub, aes(x=T, y=Sigma,fill=cut(Rtot, c(0,10,20,30,40,50,60,70,Inf))))
p<- p + geom_tile() + scale_fill_brewer(type="seq",palette = "YlGn")+facet_grid(~ptype)
p<- p + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
p

绘图如下:

Factored data

请注意,三个不同的部分像拼图一样拼凑在一起!该因子表示边界的位置。

我想在同一个图表中将这些部分组合在一起,每条区域都有一些较粗的黑线。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

尝试使用您的值复制question的答案 - 我必须稍微更改dsub才能使其正常工作(T/10(Sigma*10)+1 )...

#### I changed the values here ####
dsub$T<-dsub$T/10
dsub$Sigma<-(dsub$Sigma*10) +1
###################################

ymax <- max(dsub$Sigma)
xmax <- max(dsub$T)

m <- matrix(0, nrow=ymax, ncol=xmax)
m[as.matrix(dsub[,c(3,1)])] <- dsub[,8]

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

ggplot(data=dsub, aes(x=T,y=Sigma))+
  geom_tile(aes(fill=cut(Rtot, c(0,10,20,30,40,50,60,70,Inf))))+
  scale_fill_brewer(type="seq",palette = "YlGn")+
  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))+
  scale_x_continuous(expand=c(0,0))+
  scale_y_continuous(expand=c(0,0))+
  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())