{"id":8211,"date":"2018-11-19T22:53:19","date_gmt":"2018-11-19T20:53:19","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?p=8211"},"modified":"2019-12-25T11:14:35","modified_gmt":"2019-12-25T09:14:35","slug":"easy-way-to-expand-color-palettes-in-r","status":"publish","type":"post","link":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/","title":{"rendered":"Easy Way To Expand Color Palettes in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>This article describes how to use the <strong>colorRampPalette<\/strong>() R function to <strong>expand color palettes<\/strong>. We\u2019ll provide practical example with ggplot2.<\/p>\n<p>The color scales defined in the <a href=\"https:\/\/www.datanovia.com\/en\/blog\/top-r-color-palettes-to-know-for-great-data-visualization\/\">RColorBrewer and in other packages<\/a>, such as viridis, have a fixed number of colors.<\/p>\n<p>For example you have 8 colors in the <code>Set2<\/code> brewer palette. Consequently, if your data contain more than 8 groups, ggplot2 will return a warning like this:<\/p>\n<div class=\"warning\">\n<p>1: In brewer.pal(n, pal) : n too large, allowed maximum for palette Set2 is 8 Returning the palette you asked for with that many colors<\/p>\n<\/div>\n<p>For example, type this R code:<\/p>\n<pre class=\"r\"><code>df &lt;- iris[1:18, ]\r\ndf$name &lt;- 1:nrow(df)\r\n\r\nlibrary(ggplot2)\r\nggplot(df) + \r\n  geom_col(aes(name, Sepal.Length, fill = factor(Sepal.Length))) +\r\n  scale_fill_brewer(palette=\"Set2\") +\r\n  theme_minimal() +\r\n  theme(legend.position = \"top\")<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggplot2\/figures\/0103-expand-color-palettes-in-r-extand-color-palette-1.png\" width=\"480\" \/><\/p>\n<p>A solution is to use the function <code>colorRampPalette()<\/code> which can extend any list of colors:<\/p>\n<pre class=\"r\"><code>library(RColorBrewer)\r\n# Define the number of colors you want\r\nnb.cols &lt;- 18\r\nmycolors &lt;- colorRampPalette(brewer.pal(8, \"Set2\"))(nb.cols)\r\n# Create a ggplot with 18 colors \r\n# Use scale_fill_manual\r\nggplot(df) + \r\n  geom_col(aes(name, Sepal.Length, fill = factor(Sepal.Length))) +\r\n  scale_fill_manual(values = mycolors) +\r\n  theme_minimal() +\r\n  theme(legend.position = \"top\")<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggplot2\/figures\/0103-expand-color-palettes-in-r-extend-rcolorbrewer-palettes-1.png\" width=\"480\" \/><\/p>\n<p>In conclusion, this article describes how to expand color palettes in R.<\/p>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2. The color scales defined in the RColorBrewer and in other [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rating_form_position":"","rating_results_position":"","mr_structured_data_type":"","footnotes":""},"categories":[126],"tags":[125],"class_list":["post-8211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-tips-and-tricks","tag-r-colors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Easy Way To Expand Color Palettes in R - Datanovia<\/title>\n<meta name=\"description\" content=\"This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Easy Way To Expand Color Palettes in R - Datanovia\" \/>\n<meta property=\"og:description\" content=\"This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-19T20:53:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-25T09:14:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Alboukadel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alboukadel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\"},\"author\":{\"name\":\"Alboukadel\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\"},\"headline\":\"Easy Way To Expand Color Palettes in R\",\"datePublished\":\"2018-11-19T20:53:19+00:00\",\"dateModified\":\"2019-12-25T09:14:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\"},\"wordCount\":129,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg\",\"keywords\":[\"R Colors\"],\"articleSection\":[\"R Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\",\"name\":\"Easy Way To Expand Color Palettes in R - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg\",\"datePublished\":\"2018-11-19T20:53:19+00:00\",\"dateModified\":\"2019-12-25T09:14:35+00:00\",\"description\":\"This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Easy Way To Expand Color Palettes in R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\",\"name\":\"Alboukadel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g\",\"caption\":\"Alboukadel\"},\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/author\/kassambara\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Easy Way To Expand Color Palettes in R - Datanovia","description":"This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Easy Way To Expand Color Palettes in R - Datanovia","og_description":"This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2.","og_url":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/","og_site_name":"Datanovia","article_published_time":"2018-11-19T20:53:19+00:00","article_modified_time":"2019-12-25T09:14:35+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg","type":"image\/jpeg"}],"author":"Alboukadel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Alboukadel","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#article","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/"},"author":{"name":"Alboukadel","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e"},"headline":"Easy Way To Expand Color Palettes in R","datePublished":"2018-11-19T20:53:19+00:00","dateModified":"2019-12-25T09:14:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/"},"wordCount":129,"commentCount":5,"publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg","keywords":["R Colors"],"articleSection":["R Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/","url":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/","name":"Easy Way To Expand Color Palettes in R - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg","datePublished":"2018-11-19T20:53:19+00:00","dateModified":"2019-12-25T09:14:35+00:00","description":"This article describes how to use the colorRampPalette() R function to expand color palettes. We\u2019ll provide practical example with ggplot2.","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/DSCN1733.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/blog\/easy-way-to-expand-color-palettes-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Easy Way To Expand Color Palettes in R"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e","name":"Alboukadel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g","caption":"Alboukadel"},"url":"https:\/\/www.datanovia.com\/en\/blog\/author\/kassambara\/"}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/8211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=8211"}],"version-history":[{"count":2,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/8211\/revisions"}],"predecessor-version":[{"id":11641,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/8211\/revisions\/11641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/7959"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=8211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/categories?post=8211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/tags?post=8211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}