<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/having-bit-of-party-with-material-colour-palette/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://chichacha.netlify.com/2018/12/09/having-bit-of-party-with-material-colour-palette/"> R on Chi's Impe[r]fect Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Continuing on with my “slight?” obsession with colours… I love colours in “<a rel="nofollow" href="https://material.io/tools/color/#!/?view.left=0&view.right=0&primary.color=9C27B0" target="_blank">Material Colour Palette</a>”. There various website that will lets you grab the colours by clicking, such as this <a rel="nofollow" href="https://www.materialui.co/colors" target="_blank">one</a>, but I just wanted to have little handy cheet sheet for myself, so I’ve decided I’ll do that using R &amp; my favourite <a rel="nofollow" href="https://ggplot2.tidyverse.org/" target="_blank">ggplot2</a>.</p>
<div>
<h2>Getting colours out of image using package “imager”</h2>
<p>After quick search, I came across <a rel="nofollow" href="https://www.materialui.co/img/material-colors-thumb.png%22" target="_blank">image with all the material colour</a>, so first things I’ve tried is to get colours out of image using <a rel="nofollow" href="https://cran.r-project.org/web/packages/imager/index.html" target="_blank">imager</a>.</p>
<pre><code>## Load up packages we'll use
library(tidyverse)
library(imager)
library(patchwork)

im &lt;- load.image(&quot;https://www.materialui.co/img/material-colors-thumb.png&quot;)
#plot(im)

## Convert Image to Data Frame with HSV value
im_hsv &lt;-im %>% RGBtoHSV() %>%
  as.data.frame(wide=&quot;c&quot;) %>%
  rename(h=c.1, s=c.2, v=c.3)

## Convert Image to Data Frame with RGB value
im_rgb &lt;- im %>% 
  as.data.frame(wide=&quot;c&quot;) %>%
  rename(red=c.1,green=c.2,blue=c.3) %>%
  mutate(hexvalue = rgb(red,green,blue)) ## you can create hexvalue using red, green blue value!

## Might as well conver to grayscale, and get luminance. 
## I;ll use luminance value to decide if I'll put black text vs white text later.
im_grayscale &lt;- im %>% grayscale() %>%
  as.data.frame() %>%
  rename(luminance=value)

## I want to grab pixel from about middle of each cell
mat_color &lt;- im_rgb %>%
  filter(x %in% as.integer(round(seq(1,19)*(400/19))-10) &amp;
         y %in% as.integer(round(seq(1,11)*(225/11))-10) &amp; y>10) %>%
  left_join(im_hsv) %>%
  left_join(im_grayscale) %>%
  mutate_at(c(&quot;x&quot;,&quot;y&quot;), dense_rank) %>%
  arrange(x,y) 


col_group &lt;- c(&quot;red&quot;,&quot;pink&quot;,&quot;purple&quot;,&quot;deep purple&quot;,&quot;indigo&quot;,&quot;blue&quot;,&quot;light blue&quot;,&quot;cyan&quot;,&quot;teal&quot;,&quot;green&quot;,&quot;light green&quot;,&quot;lime&quot;,&quot;yellow&quot;,&quot;amber&quot;,&quot;orange&quot;,&quot;deep orange&quot;,&quot;brown&quot;,&quot;grey&quot;,&quot;blue grey&quot;)

## Adding extra info to the table 
mat_color &lt;- mat_color %>% 
  mutate(hue_group=factor(x, labels=col_group, ordered=T),
         shade = factor(y, labels=c(50,seq(100,900, by=100)), ordered=T))

## I could also save this as csv file too... :) 
#mat_color %>% write_csv(&quot;MaterialColour.csv&quot;)</code></pre>
</div>
<div>
<h2>Creating Material Colour Palette Cheat Sheet</h2>
<p>Now that I have the colours out of image in data frame, I can do fun stuff, plotting!! I should print this with colour printer, and have it as <a rel="nofollow" href="https://www.rstudio.com/resources/cheatsheets/" target="_blank">one of cheat sheet collection</a>.</p>
<p>I’ve used luminance value of each colour to decide if I should place black text or white text over the colour. (I couldn’t figure out if there’s good rules to follow, but seems like luminance does the trick?!)</p>
<pre><code>mat_color %>% 
  ggplot(aes(x=hue_group,y=shade)) +  ## I could also use x=x,y=y
  geom_tile(aes(fill=hexvalue),color=&quot;white&quot;, size=0.1) +  ## i want to have very fine white line around each tiles.
  scale_fill_identity(guide=&quot;none&quot;) +  
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  ## print out color hue name and shade
  geom_text(aes(label=paste0(hue_group,&quot;-&quot;,shade), 
                color=ifelse(luminance>0.5,&quot;#000000&quot;,&quot;#ffffff&quot;)), ## about 48% opacity
            family=&quot;Roboto Condensed&quot;, size=3, vjust=-2, lineheight=0.8, alpha=0.48) +
  ## print out hesvalue - I'll use this the most, so print it with higher transparency
  geom_text(aes(label=hexvalue, 
                color=ifelse(luminance>0.5,&quot;#000000&quot;,&quot;#ffffff&quot;)),  ## about 80% opacity
            family=&quot;Roboto Condensed&quot;, fontface=&quot;bold&quot;,vjust=0, alpha=0.8) +
  ## print out RGB &amp; HSV
  geom_text(aes(label=paste0(&quot;\n(&quot;,round(red*255),&quot;,&quot;,round(blue*255),&quot;,&quot;,round(green*255),&quot;)\n(&quot;,
                             round(h),&quot;,&quot;,round(s,1),&quot;,&quot;,round(v,1),&quot;)&quot;), 
                color=ifelse(luminance>0.5,&quot;#000000&quot;,&quot;#ffffff&quot;)), ## about 67% opacity
            family=&quot;Roboto Condensed&quot;, size=3, vjust=1, lineheight=0.8, alpha=0.67) +
  scale_color_identity() +
  labs(x=&quot;&quot;,y=&quot;&quot;,title=&quot;&quot;, caption=&quot;Material Colour Palettes Hexvalue + RGB + HSV&quot;)</code></pre>
<p><img src="https://i0.wp.com/chichacha.netlify.com/post/2018-12-09-having-bit-of-party-with-material-colour-palette_files/figure-html/materialColourCheatSheet-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>## I can save as PNG file too with below line
#ggsave(&quot;MaterialColorCheatSheet.png&quot;, width=16, height=9)</code></pre>
</div>
<div>
<h2>More Parrrty Time with Colours…</h2>
<p>While plotting colour in rectangular is good…. I thought it’s a lot nicer to plot them as “<a rel="nofollow" href="https://en.wikipedia.org/wiki/Color_wheel" target="_blank">Colour Wheel</a>”.</p>
<pre><code># Colour Wheel! 
wheel_base &lt;-mat_color %>% 
  filter(!hue_group %in% c(&quot;brown&quot;,&quot;grey&quot;,&quot;blue grey&quot;)) %>%  ## exclude brown, grey and blue grey group.
  ggplot(aes(x=x, y=y)) +
  geom_tile(aes(fill=hexvalue), color=&quot;white&quot;, size=0.1) +
  scale_fill_identity(guide=&quot;none&quot;) +
  coord_polar() +  ## Converting to polar coordinate does the trick!
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  labs(caption=&quot;Color Wheel using Material Design Colours&quot;)


## Just experimenting with smaller strips on each colour tiles..
wheel_base_w &lt;- wheel_base +
  geom_tile(fill=&quot;#ffffffde&quot;,height=0.5, aes(width=v*0.5)) +
  labs(caption=&quot;If you were to play white text... \nWhich ones can you see better?&quot;)

wheel_base_b &lt;- wheel_base +
  geom_tile(fill=&quot;#000000de&quot;,height=0.5, aes(width=v*0.5)) +
  labs(caption=&quot;If you were to play black text... \nWhich ones can you see better?&quot;)

## using &quot;patchwork&quot; package I can plot all 3 charts next to each other.
wheel_base + wheel_base_w + wheel_base_b</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-12-09-having-bit-of-party-with-material-colour-palette_files/figure-html/ColourWheel-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>I wanted to see if I can find pairs of colour that I like by shuffling the colours on smaller strips for fun too.</p>
<pre><code>## Randomize Y
a&lt;-wheel_base +
  geom_tile(aes(fill=hexvalue,y=sample(y)), width=0.5, height=0.5) +
  labs(caption=&quot;Randomness within Hue Group&quot;)

b&lt;-wheel_base +
  geom_tile(aes(fill=hexvalue,x=sample(x)), width=0.5, height=0.5) +
  labs(caption=&quot;Randomness within Same Shade&quot;)

## Random Colour Wheel
c&lt;-wheel_base +
  geom_tile(aes(fill=hexvalue,x=sample(x), y=sample(y,replace=T)), width=0.5, height=0.5) +
  labs(caption=&quot;Randomness to see if i spot any pairs I like&quot;)

a+b+c</code></pre>
<p><img src="https://i2.wp.com/chichacha.netlify.com/post/2018-12-09-having-bit-of-party-with-material-colour-palette_files/figure-html/ColourWheel2-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Making Some Flowers with Material Colour Palette</h2>
<pre><code>## Just for fun, let's just make some flower with material colour palette!
flower1&lt;-mat_color %>%
  arrange(shade,hue_group) %>% mutate(t=row_number()) %>%
  ggplot(aes(x=sqrt(t) * cos(t), y=sqrt(t) * sin(t))) +
  geom_point(aes(color=hexvalue, size=luminance)) +
  #geom_text(aes(label=t), family=&quot;Avenir&quot;) +
  scale_color_identity() +
  coord_fixed() +
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  scale_size_continuous(range=c(3,8), guide=&quot;none&quot;) +
  labs(caption=&quot;Sort by Shade, then Hue Group, Luminance as Size&quot;)


## Just for fun, let's just make some flower with material colour palette!
flower2&lt;-mat_color %>%
  arrange(hue_group,shade) %>% mutate(t=row_number()) %>%
  ggplot(aes(x=sqrt(t) * cos(t), y=sqrt(t) * sin(t))) +
  geom_point(aes(color=hexvalue, size=luminance)) +
  scale_color_identity() +
  coord_fixed() +
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  scale_size_continuous(range=c(3,8), guide=&quot;none&quot;) +
  labs(caption=&quot;Sort by Hue Group, Then Shade, Luminance as Size&quot;)

## ggplot2 has shape 1-25
## 0-15 &amp; 20-24 are NOT filled shape

flower3&lt;-mat_color %>%
  arrange(shade, hue_group) %>% mutate(t=row_number()) %>%
  ggplot(aes(x=sqrt(t) * cos(t), y=sqrt(t) * sin(t))) +
  geom_point(aes(color=hexvalue, shape=ifelse(x&lt;=15,x-1,x+5)), size=3, stroke=1.5) +
  scale_color_identity() +
  coord_fixed() +
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  scale_shape_identity() +
  labs(caption=&quot;Mapping Hue Groups to Shapes for Fun&quot;)

flower1 + flower2 + flower3</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-12-09-having-bit-of-party-with-material-colour-palette_files/figure-html/FlowerLikeArt-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>One last one for now…</h2>
<pre><code>## Final Random Art
mat_color %>% 
  ggplot(aes(x=x,y=y)) + 
  geom_tile(aes(fill=hexvalue, width=s*10, height=v*10, alpha=luminance)) +
  scale_fill_identity() +
  scale_alpha_continuous(guide=&quot;none&quot;, range=c(0,0.6)) +
  theme_void() +
  annotate(x=(max(mat_color$x)/2)+0.05,y=(max(mat_color$y)/2)-0.05, 
           label=&quot;The End&quot;,
           geom=&quot;text&quot;, family=&quot;Roboto Condensed&quot;, size=22, color=&quot;#000000de&quot;) +
  annotate(x=max(mat_color$x)/2,y=max(mat_color$y)/2, 
           label=&quot;The End&quot;,
           geom=&quot;text&quot;, family=&quot;Roboto Condensed&quot;, size=22, color=&quot;#ffffffde&quot;) +
  coord_cartesian(xlim=c(0,19), ylim=c(3,7))</code></pre>
<p><img src="https://i2.wp.com/chichacha.netlify.com/post/2018-12-09-having-bit-of-party-with-material-colour-palette_files/figure-html/TheEnd-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://chichacha.netlify.com/2018/12/09/having-bit-of-party-with-material-colour-palette/"> R on Chi's Impe[r]fect Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/ymNcEzsaY3g" height="1"></a></div>