Assigning Colors Across Multiple Charts Using ggplot2
When creating dashboards, we often would like to ensure that the same colors correspond to the same series across multiple charts. This could be desirable because our series could have associated brand colors, or simply to ensure continuity and effective use of color. ("The red bar on one chart is the same series as the red bar in my other chart.")
While we can definitely use Periscope Data's visualization option to assign colors strategically, we can leverage SQL Views and ggplot2's scale_fill_manual function to more easily apply the same color scheme consistently.
Here's the query for a view, called source_color_key, for different sources in a fictional gaming company.
select 'admob' as source,'magenta' as color union all select 'leadbolt' as source, 'navy' as color union all select 'organic' as source, 'olivedrab' as color union all select 'tapjoy' as source, 'darkgoldenrod' as color
Step 2: Join your SQL output to your color key view
Step 3: Use ggplot's scale_fill_manual parameter, as shown below, to assign your custom values to your series. Remember to create a named vector that contains the name, color of each series value.
library(ggplot2) cols=df$color names(cols)=df$source a=ggplot(df,aes(x=source,y=count,fill=source)) + geom_bar(stat="identity") + scale_fill_manual(values=cols) periscope.image(a)
The final result looks like this!
Note that if we are even missing a series entirely, we still preserve the assigned coloring. In the graph below, we don't have any data for the "organic" source, but none of the other colors have changed due to our named vector.
Any other tips and tricks you like to use for coloring your graphs?