Note that there are some explanatory texts on larger screens.

plurals
  1. POscale_colour_manual R ggplot2 geom_tile() missing something?
    primarykey
    data
    text
    <p>G'day Everyone,</p> <p>I am having a trouble figuring out how to specify the 'fill' colours for my graphs (below).</p> <p>The different subsets of my data have different levels when I specify my 'fill' variable, e.g. one subset has 2 levels, and the other two have three levels. When I tell my plot to use the variable to 'fill' my graphs colours I get different colours between my graphs. I have tried to specify which colours my 'fill' variable levels should use but it hasn't worked. I have tried using multiple rearrangements of scale_colour_manual(), and a few other things I found around SO, but haven't been able to figure it out.</p> <p>Could someone please point out where I am going wrong, when trying to specify my colours. Below is a representation of the code I am using and an image of my problem. </p> <pre><code># Code to post on Stack overflow to see where I am going wrong with scale_colour_manual() data &lt;- data.frame('id' = c('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'), 'treatment' = c('1', '1', '1','1', '1', '1','1', '1', '1', '2', '2', '2','2', '2', '2','2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3'), 'loci' = c('1', '1', '1', '2', '2', '2', '3', '3', '3', '1', '1', '1', '2', '2', '2', '3', '3', '3', '1', '1', '1', '2', '2', '2', '3', '3', '3'), 'assign' = c('0', '0', '0', '1', '2', '1', '1', '1', '1', '0', '0', '0', '1', '2', '1', '1', '1', '2', '0', '0', '0', '1', '2', '2', '1', '1', '1')) a &lt;- data[which(data$id == 'a'),] b &lt;- data[which(data$id == 'b'),] c &lt;- data[which(data$id == 'c'),] aheat &lt;- ggplot(a, aes(as.factor(loci), as.factor(treatment), fill = factor(assign))) + geom_tile(colour = 'white') + scale_x_discrete(expand = c(0,0)) + scale_y_discrete(expand = c(0,0)) + xlab("Number of Loci") + ylab("Treatment") + ggtitle("A Plot") + theme(axis.ticks = element_blank(), axis.text.x = element_text(angle = 320, size = 6, vjust = 0.5), axis.text.y = element_text(size = 8)) + scale_colour_manual(breaks = a$assign, values = c('0' = 'blue', '1' = 'green')) aheat bheat &lt;- ggplot(b, aes(as.factor(loci), as.factor(treatment), fill = factor(assign))) + geom_tile(colour = 'white') + scale_x_discrete(expand = c(0,0)) + scale_y_discrete(expand = c(0,0)) + xlab("Number of Loci") + ylab("Treatment") + ggtitle("B Plot") + theme(axis.ticks = element_blank(), axis.text.x = element_text(angle = 320, size = 6, vjust = 0.5), axis.text.y = element_text(size = 8)) + scale_colour_manual(breaks = b$assign, values = c('0' = 'blue', '1' = 'green', '2' = 'red')) bheat cheat &lt;- ggplot(c, aes(as.factor(loci), as.factor(treatment), fill = factor(assign))) + geom_tile(colour = 'white') + scale_x_discrete(expand = c(0,0)) + scale_y_discrete(expand = c(0,0)) + xlab("Number of Loci") + ylab("Treatment") + ggtitle("C Plot") + theme(axis.ticks = element_blank(), axis.text.x = element_text(angle = 320, size = 6, vjust = 0.5), axis.text.y = element_text(size = 8)) + scale_colour_manual(breaks = c$assign, values = c('0' = 'blue', '1' = 'green', '2' = 'red')) cheat </code></pre> <p><img src="https://i.stack.imgur.com/TITxC.png" alt="enter image description here"></p> <p>Thanks for all the help!</p> <p>Regards, Adam</p>
    singulars
    1. This table or related slice is empty.
    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.
    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