{"id":15541,"date":"2020-03-29T15:22:11","date_gmt":"2020-03-29T14:22:11","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?p=15541"},"modified":"2020-03-29T15:22:11","modified_gmt":"2020-03-29T14:22:11","slug":"how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally","status":"publish","type":"post","link":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/","title":{"rendered":"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally"},"content":{"rendered":"<div class=\"rdoc\">\n<pre class=\"r\"><code># Load required R packages\r\nsuppressPackageStartupMessages(library(ggpubr))\r\nsuppressPackageStartupMessages(library(dplyr))\r\n\r\n# Example of data from statistical tests\r\nstat.test &lt;- tibble::tribble(\r\n  ~supp, ~group1, ~group2,  ~p.adj, ~p.signif,\r\n   \"VC\",   \"0.5\",     \"1\", 3.4e-06,    \"****\",\r\n   \"VC\",   \"0.5\",     \"2\", 2.8e-07,    \"****\",\r\n   \"VC\",     \"1\",     \"2\", 0.00026,    \"****\",\r\n   \"OJ\",   \"0.5\",     \"1\", 0.00026,    \"****\",\r\n   \"OJ\",   \"0.5\",     \"2\", 5.3e-06,    \"****\",\r\n   \"OJ\",     \"1\",     \"2\",   0.039,       \"*\"\r\n  )\r\n\r\n# Create a ggtextable\r\nggtab &lt;- ggtexttable(stat.test, rows = NULL, theme = ttheme(\"classic\"))\r\n\r\n# Specify colors for significant p-values\r\nstat.test &lt;- stat.test %&gt;% \r\n  mutate(\r\n    fill = ifelse(p.adj &lt; 0.01, \"green\", \"gray\"),\r\n    color = \"black\"\r\n    )\r\nstat.test <\/code><\/pre>\n<pre><code>## # A tibble: 6 x 7\r\n##   supp  group1 group2      p.adj p.signif fill  color\r\n##   &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;       &lt;dbl&gt; &lt;chr&gt;    &lt;chr&gt; &lt;chr&gt;\r\n## 1 VC    0.5    1      0.0000034  ****     green black\r\n## 2 VC    0.5    2      0.00000028 ****     green black\r\n## 3 VC    1      2      0.000260   ****     green black\r\n## 4 OJ    0.5    1      0.000260   ****     green black\r\n## 5 OJ    0.5    2      0.0000053  ****     green black\r\n## 6 OJ    1      2      0.039      *        gray  black<\/code><\/pre>\n<pre class=\"r\"><code># Coloring the table conditionnally using `ggpubr::table_cell_bg()`\r\nfor(i in 1:nrow(stat.test)){\r\n  row = i+1\r\n  column = which(colnames(stat.test) == \"p.adj\")\r\n  ggtab &lt;- table_cell_bg(\r\n    ggtab, row = row, column = column,\r\n    fill = stat.test$fill[i], color = stat.test$color[i]\r\n    )\r\n}\r\nprint( ggtab)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/mBJddTW.png\" alt=\"plot of chunk draw-textual-table-and-color-cells\" \/><\/p>\n<pre class=\"r\"><code># Read more: https:\/\/rpkgs.datanovia.com\/ggpubr\/reference\/ggtexttable.html<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p># Load required R packages suppressPackageStartupMessages(library(ggpubr)) suppressPackageStartupMessages(library(dplyr)) # Example of data from statistical tests stat.test &lt;- tibble::tribble( ~supp, ~group1, ~group2, ~p.adj, ~p.signif, &#8220;VC&#8221;, &#8220;0.5&#8221;, &#8220;1&#8221;, 3.4e-06, &#8220;****&#8221;, &#8220;VC&#8221;, &#8220;0.5&#8221;, &#8220;2&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15542,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rating_form_position":"","rating_results_position":"","mr_structured_data_type":"","footnotes":""},"categories":[341],"tags":[343],"class_list":["post-15541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-faq","tag-ggpubr"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Draw a Textual Table with GGPLOT and Color Cells Conditionally - Datanovia<\/title>\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\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally - Datanovia\" \/>\n<meta property=\"og:description\" content=\"# Load required R packages suppressPackageStartupMessages(library(ggpubr)) suppressPackageStartupMessages(library(dplyr)) # Example of data from statistical tests stat.test &lt;- tibble::tribble( ~supp, ~group1, ~group2, ~p.adj, ~p.signif, &quot;VC&quot;, &quot;0.5&quot;, &quot;1&quot;, 3.4e-06, &quot;****&quot;, &quot;VC&quot;, &quot;0.5&quot;, &quot;2&quot;, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-29T14:22:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png\" \/>\n\t<meta property=\"og:image:width\" content=\"432\" \/>\n\t<meta property=\"og:image:height\" content=\"288\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\"},\"author\":{\"name\":\"Alboukadel\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\"},\"headline\":\"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally\",\"datePublished\":\"2020-03-29T14:22:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\"},\"wordCount\":12,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png\",\"keywords\":[\"ggpubr\"],\"articleSection\":[\"FAQ\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\",\"name\":\"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png\",\"datePublished\":\"2020-03-29T14:22:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png\",\"width\":432,\"height\":288,\"caption\":\"plot of chunk draw-textual-table-and-color-cells\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally\"}]},{\"@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":"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally - Datanovia","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\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/","og_locale":"en_US","og_type":"article","og_title":"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally - Datanovia","og_description":"# Load required R packages suppressPackageStartupMessages(library(ggpubr)) suppressPackageStartupMessages(library(dplyr)) # Example of data from statistical tests stat.test &lt;- tibble::tribble( ~supp, ~group1, ~group2, ~p.adj, ~p.signif, \"VC\", \"0.5\", \"1\", 3.4e-06, \"****\", \"VC\", \"0.5\", \"2\", [&hellip;]","og_url":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/","og_site_name":"Datanovia","article_published_time":"2020-03-29T14:22:11+00:00","og_image":[{"width":432,"height":288,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png","type":"image\/png"}],"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\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#article","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/"},"author":{"name":"Alboukadel","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e"},"headline":"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally","datePublished":"2020-03-29T14:22:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/"},"wordCount":12,"commentCount":0,"publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png","keywords":["ggpubr"],"articleSection":["FAQ"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/","url":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/","name":"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png","datePublished":"2020-03-29T14:22:11+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/03\/mBJddTW.png","width":432,"height":288,"caption":"plot of chunk draw-textual-table-and-color-cells"},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-draw-a-textual-table-with-ggplot-and-color-cells-conditionally\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to Draw a Textual Table with GGPLOT and Color Cells Conditionally"}]},{"@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\/15541","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=15541"}],"version-history":[{"count":1,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/15541\/revisions"}],"predecessor-version":[{"id":15544,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/15541\/revisions\/15544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/15542"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=15541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/categories?post=15541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/tags?post=15541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}