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.

Step 1: Create a SQL view that assigns your series to R colors.  R has many exciting options to choose from - a list of supported colors can be found here!

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.



a=ggplot(df,aes(x=source,y=count,fill=source)) + geom_bar(stat="identity") + scale_fill_manual(values=cols)


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?

1reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Very clever; for those not using Python (or who don't want to generate images for all their charts) a way to do this natively in Periscope would be appreciated, as well.

    Reply Like 1
Like Follow
  • 8 mths agoLast active
  • 1Replies
  • 541Views
  • 2 Following