[R] aligning image with bar plot in the same plot

Luís F respostas17 at gmail.com
Tue Jul 17 21:55:16 CEST 2012


Dear Uwe, Dear List,

This is exactly it. I knew there had to be an easier way. And I had
even looked up in par for it! Knowledge is a beautiful thing; thank
you so much.

> On Tue, Jul 17, 2012 at 4:28 PM, Uwe Ligges
> <ligges at statistik.tu-dortmund.de> wrote:
>
> Insert
>
> par(yaxs="i")
>
> after you are finsihed with the image.
> See ?par for details what is does.
>
> Uwe Ligges

>> On 17.07.2012 13:13, Tiago R M wrote:

>>>
>>> Dear Mailing list,
>>>
>>> I want to plot a matrix using image() and on its side I want to give
>>> information to every line of the matrix using colors. I tried to use
>>> barplot, but cannot align the two plots. Code below. I succeed in doing
>>> what I want using another image() plot, but was wondering whether there
>>> is
>>> an easier way to do it.
>>>
>>> Thank you so much!
>>>
>>> Tiago
>>>
>>> ############# code
>>>
>>> ## matrix to plot
>>> matI <- matrix(c(rep(1:3, 100), rep(4:6, 100), rep(7:9, 100)), nrow=3,
>>> byrow=T)
>>>
>>> ## plot with three panels
>>> layout(matrix(1:3, nrow=1), widths=c(1,1,1))
>>>
>>> #  margins
>>> mar1 <- c(1,1,1,1)
>>> par(mar=mar1)
>>>
>>> ## plot matrix
>>> image(x=(1:(ncol(matI)+1)), y=(1:(nrow(matI)+1)), t(matI), axes=F)
>>> abline(h=c(1,100,200,300), xpd=NA)
>>>
>>> ## manually adjusting bottom and up par it lines up
>>> ## mar1 <- c(1.9,0,1.9,2)
>>> ## par(mar=mar1)
>>> colorsBarPlot <- c(rep('green', 100), rep('blue', 100), rep('black',
>>> 100))
>>> barplot(rep(1, 300), space=0, border=NA, col=colorsBarPlot, horiz=T,
>>> axes=F)
>>>
>>> # it works but seems to cumbersome
>>> codeCol <- as.numeric(as.factor(colorsBarPlot))
>>> colsImage <- unique(colorsBarPlot)
>>> colsBreaks <- sort(unique(c(codeCol-0.5, codeCol+0.5)))
>>> image(matrix(codeCol, nrow=1), col=rev(colsImage), breaks=colsBreaks,
>>> axes=F)
>>>
>>> ######### end of code
>>>
>>>         [[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.



More information about the R-help mailing list