Note that there are some explanatory texts on larger screens.

plurals
  1. POMixing colours to make a custom plot in R
    primarykey
    data
    text
    <p>UPDATE: Deleted now irrelevant preamble and updated progress.</p> <p>Hi I'm trying to plow a horizontal bar a bit like a heat-map, which will change color based on values below. The bar should be red by default, but changes color depending on the values of two parameters.</p> <p>I've found some pages that try for a similar effect on SO: <a href="https://stackoverflow.com/questions/12081843/r-matrix-plot-with-colour-threshold-and-grid">R matrix plot with colour threshold and grid</a></p> <p><a href="https://stackoverflow.com/questions/11773295/how-can-one-mix-2-or-more-color-palettes-to-show-a-combined-color-value">How can one mix 2 or more color palettes to show a combined color value</a></p> <p>So I figure the best way to go about this would be to plot a series of tiles horizontally, and each one has a calculated color based on two values.</p> <p>What I have managed to do, is generate the colors I want. I did this by getting a matrix[2xn], where n is the number of tiles. row1 contains value1 for each tile, and row 2 contains value 2 for each tile:</p> <pre><code> colourmatrix[,c(1:5)] [,1] [,2] [,3] [,4] [,5] "81" "82" "82" "82" "82" "58" "58" "58" "58" "58" </code></pre> <p>I then generated the colors for each tile by applying the following to all columns:</p> <pre><code>col.deter &lt;- function(invalues) { cols &lt;- rgb(green=invalues[1], red=100, blue=invalues[2], maxColorValue=100) return(cols) } cols &lt;- unlist(lapply(1:ncol(colourmatrix), function(i) col.deter(colourmatrix[,i]))) </code></pre> <p>Then I do an rbind() my results is:</p> <pre><code> &gt; colourmatrix[,c(1:5)] [,1] [,2] [,3] [,4] [,5] "81" "82" "82" "82" "82" "58" "58" "58" "58" "58" cols "#FFCF94" "#FFD194" "#FFD194" "#FFD194" "#FFD194" </code></pre> <p>So for each tile, the higher value 1, the more green is added to the mix, the higher value 2, the more blue is added to the mix. Thus the color for every tile is determined and added as a third row.</p> <p>What I want to do now, is something like the grid generated in this page with ggplot2 in the answers with this code:</p> <pre><code>dat &lt;- expand.grid(blue=seq(0, 100, by=10), red=seq(0, 100, by=10)) dat &lt;- within(dat, mix &lt;- rgb(green=0, red=red, blue=blue, maxColorValue=100)) library(ggplot2) ggplot(dat, aes(x=red, y=blue)) + geom_tile(aes(fill=mix), color="white") + scale_fill_identity() </code></pre> <p>but imagine only one line. I'm trying to achieve one line of tiles going left to right, their color determined by the values generated.</p> <p>I have attempted to do this with ggplot2 and image() but my attempts fall flat, and I don't normally use ggplot 2 I don't understand aes and all the errors I get, but I tried it since that's what was used to generate the red and blue tile plot produced by the code chunk above.</p> <p>My failed attempts:</p> <pre><code>ggplot(c(1,ncol(colourmatrix)), 1, aes(x=x, y=y)) + geom_tile(aes(fill=colourmatrix[3,]), color="white") + scale_fill_identity() Error in ggplot.data.frame(fortify(data, ...), mapping) : Mapping should be created with aes or aes_string qplot(c(1,ncol(colourmatrix)), c(1,ncol(colourmatrix)), fill=colourmatrix[3,], geom='tile') Error: Aesthetics must either be length one, or the same length as the dataProblems:c(1, ncol(colourmatrix)), c(1, ncol(colourmatrix)) </code></pre> <p>I've also got some failed attempts trying image() pages I've read to try and achieve this are:</p> <p><a href="https://stackoverflow.com/questions/11773295/how-can-one-mix-2-or-more-color-palettes-to-show-a-combined-color-value">How can one mix 2 or more color palettes to show a combined color value</a></p> <p><a href="https://stackoverflow.com/questions/12081843/r-matrix-plot-with-colour-threshold-and-grid">R matrix plot with colour threshold and grid</a></p> <p>Thanks,</p> <p>Ben W.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload