[R] image/area plot

Henrique Dallazuanna wwwhsd at gmail.com
Tue Jan 22 11:51:58 CET 2008


Perhaps you can do this:


On 21/01/2008, Marta Rufino <mrufino at cripsul.ipimar.pt> wrote:
>
>  Thank you very much for the help.
>
>
>
>  kk=data.frame(fact=letters[1:10],
> freq=c(5,1,10,2,10,7,5,10,30,20))
> res <- rep(kk[[1]], kk[[2]])
> resmat <- matrix(c(res), 10)
> image(1:10, 1:10, resmat, col=rainbow(20))
> grid(ncol(resmat), nrow(resmat))
>
>
>  Great! that is much better :-)
>  Still,... any idea about how to rearrange the matrix, in a way that the
> area of each letter is together (for example, in the fig. area 'f' and 'e'
> were split into two lines :-( )?


res1 <- do.call('rbind', lapply(split(c(res), c(res)),
function(x)append(x, rep(NA, max(table(res))-length(x)))))
image(t(res1), col=rainbow(20))
grid(30,10)


>
>
>  ##1
> text(expand.grid(seq_len(ncol(resmat)), seq_len(nrow(resmat))),
> as.character(res))
>
>  Excelent, this is very interesting code. Still, I wanted to have only one
> legend in each area.

pos1 <- t(sapply(unique(resmat), function(x)which(resmat==x, arr.ind=T)[1,]))
text(apply(pos1[!duplicated(pos1),][,-3], 2, as.numeric), labels=unique(res))



>
>  ##2
> pos <- lapply(apply(apply(resmat, 2, duplicated), 2,
> function(x)which(!x)), append, 11)
> for(j in 1:length(pos)){
>  rect(pos[[j]]-.5, j-.5, pos[[j]]-.5, j+.5, lwd=2)
> }
> abline(h=c(seq(1.5, 5.5, by=1), 8.5), lwd=2)
>
>
>  This is exactly it!!!
>
>
>  ##3
> plot(0, xlim=c(0,10), ylim=c(1, max(table(resmat))), type="n", xaxt="n")
> tb <- table(resmat)
> dis <- 0
> for(i in 1:10){
> rect(dis, seq(par("usr")[1], tb[i], by=1), dis+.8, seq(par("usr")[1],
> tb[i], by=1)+1)
> dis <- dis + 1
> }
> axis(1, at=0.5:9.5, labels=LETTERS[1:10])

image(1:10, 1:10, resmat, col=rainbow(20))
grid(ncol(resmat), nrow(resmat))
pos <- lapply(apply(apply(resmat, 2, duplicated), 2,
function(x)which(!x)), append, 11)
for(j in 1:length(pos)){
	rect(pos[[j]]-.5, j-.5, pos[[j]]-.5, j+.5, lwd=7, col="white",
border=T, density=100)
}
abline(h=c(seq(1.5, 5.5, by=1), 8.5), lwd=7, col="white")

>
>  What I meant was using the image plot (or something similar). Maybe it is
> easier to show some examples in the net:... here is an example:
>
>  http://grammarpolice.net/archives/001387.php
>
>  I guess this could be accomplished by re-ordering the matrix values, so
> that the values are not in rows but clustered into boxes, I guess... but how
> to do this?
>
>
>  ##4
> I think that symbols function should be useful
>
>  points(expand.grid(seq_len(ncol(resmat)), seq_len(nrow(resmat))), c(res))
> #using your excelent code, I think this makes the job :-)
>
> Thank you very much,
> Best wishes,
> Marta
>
>
>  On 18/01/2008, Marta Rufino <mrufino at cripsul.ipimar.pt> wrote:
>
>
>  Yes, that is it, a square pie chart :-) I did not knew the name... sorry...
>
> Does anyone knows about it?
> Thank you very much,
> Best wishes,
> Marta
>
> hadley wickham wrote:
>
>
>  Do you have an example graphic that shows what you're trying to
> create? I can't figure out if you want something like a square pie
> chart (aka waffle chart), a stacked barchart, a levelplot, or
> something else.
>
> Hadley
>
> On Jan 18, 2008 6:06 AM, Marta Rufino <mrufino at cripsul.ipimar.pt> wrote:
>
>
>
>  Dear R users,
>
> I am trying to produce an image plot, that represents the proportions of
> a factor (z variable), so that the number of squares of each colour
> represents each factor level, with the respective label inside (sorry
> for the crap English).
>
> # Something like this:
>
> kk=data.frame(fact=letters[1:10],
> freq=c(5,1,10,2,10,7,5,10,30,20)) #
> factor and respective frequecies
> res="a" # transform into a matrix (is there an easier way to do this?...
> for the image plot
> for(ii in 1:dim(kk)[1]){
>  res=c(res, rep(as.character(kk[ii,1]), l=kk[ii, 2]))
>  }
> res=res[-1]
> res
> res=matrix(c(factor(res)), nc=10)
>
> image(x=1:10,y=1:10, res[,order(colSums(res))], col=rainbow(20))
> abline(h=seq(0.5,10.5,1), col=8, lty=3); abline(v=seq(0.5,10.5,1),
> col=8, lty=3)#add some gridlines
>
> #Problems:
> #1. How to add the labels in each area
> text(1,1,paste("Factor level",kk[1,1]), pos=4)
>
> #2. How to separate the areas of each factor level (for BW printing),
> with lines (instead of colours as it is)
> segments(.5,1.5,5.5,1.5, lwd=2);segments(5.5,.5,5.5,1.5, lwd=2) #
> something like this, but automatically :-(
>
> #3. How to keep the areas together (contiguous) and not allow split over
> to lines?
>
> #4. Could I replace the col. by a symbol, for example? I think I would
> need to use plot instead of image.
> Any help will be much apretiated,
>
> Thank you very much in advance,
> Best wishes,
> Marta
> PS: This type of graphs are used in community ecology analysis, for
> example-...
>
> --
> .......................................................................
> Marta M. Rufino (PhD)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>
>
>  --
> .......................................................................
> Marta M. Rufino (PhD)
>
> .....
> Instituto Nacional de Investigação Agrária e das Pescas (INIAP/IPIMAR),
> Centro Regional de Investigação Pesqueira do Sul (CRIPSul)
> Avenida 5 de Outubro s/n
> P-8700-305 Olhão, Portugal
> +351 289 700 541
>
> .....
> Institut de Ciències del Mar - CMIMA (CSIC)
> Passeig Marítim de la Barceloneta, 37-49
> 08003 BARCELONA - Catalunya
> Spain
>
>
>
>
>
>
>  [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>
>
>  --
> .......................................................................
> Marta M. Rufino (PhD)
>
> .....
> Instituto Nacional de Investigação Agrária e das Pescas (INIAP/IPIMAR),
> Centro Regional de Investigação Pesqueira do Sul (CRIPSul)
> Avenida 5 de Outubro s/n
> P-8700-305 Olhão, Portugal
> +351 289 700 541
>
> .....
> Institut de Ciències del Mar - CMIMA (CSIC)
> Passeig Marítim de la Barceloneta, 37-49
> 08003 BARCELONA - Catalunya
> Spain
>
>
>
>
>
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list