Note that there are some explanatory texts on larger screens.

plurals
  1. POManually setting group colors for ggplot2
    primarykey
    data
    text
    <p>I have a data set from which I have to make several dozen plots. The data consists of 30 Groups and several measurements within each group. </p> <p>Most plots will not use all the groups at once. </p> <p>My goal is to have one single color pallete for the set of groups such that any given group is the same color across all graphs. In the example below, this would mean that <code>Group C</code> is the same color in <code>Plot 1</code> and in <code>Plot 2</code>. </p> <p>My question is how to go about this. </p> <p>I've tried several variations of scale_fill_manual (and scal_color_manual, when appropriate). While the colors are chosen from the designated pallete, I cannot seem to index the pallette by the group, or some other way to "skip" over the colors corresponding to a missing group</p> <p>I also thought to try to add the color information as a column of the data being plotted, but I'm unsure how to then use this column as the color for the group. </p> <p><img src="https://i.stack.imgur.com/3NQ7D.png" alt="Plot Example"></p> <pre><code># SAMPLE DATA: DT1 &lt;- data.table(Name=c("C_sub1", "A_sub1", "A_sub2"), Value=c(2,5,3), Group=c("C", "A", "A"), key="Group") DT2 &lt;- data.table(Name=c("A_sub1", "B_sub1", "C_sub1", "C_sub2"), Value=c(4,3,6,3), Group=c("A", "B", "C", "C"), key="Group") # SAMPLE Color Pallette ColorsDT &lt;- data.table(Group=LETTERS[1:5], Color=c("#333BFF", "#CC6600", "#9633FF", "#E2FF33", "#E3DB71"), key="Group") # Add a column for Color, according to the Group DT1[ColorsDT, Color := i.Color] DT2[ColorsDT, Color := i.Color] # A Basic Plot simplePlot &lt;- function(DT, tit) ggplot(DT ,aes(x=Name, y=Value, fill=Group)) + geom_bar(stat="identity") + xlab("") + ggtitle(tit) # Tried sevearl variations of: # + scale_fill_manual(values=ColorsDT$Color) # Plot Them grid.arrange(ncol=2, simplePlot(DT1, tit="Plot 1"), simplePlot(DT2, tit="Plot 2")) </code></pre>
    singulars
    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.
 

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