{"id":1958,"date":"2021-05-24T07:28:16","date_gmt":"2021-05-24T07:28:16","guid":{"rendered":"https:\/\/bi.unija.com\/en\/?p=1958"},"modified":"2021-05-24T07:28:40","modified_gmt":"2021-05-24T07:28:40","slug":"ggplot2-with-table-of-values","status":"publish","type":"post","link":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/","title":{"rendered":"ggplot2 With Table of Values"},"content":{"rendered":"<p>When plotting charts, we often want to see the actual values listed as a table. Bellow we created <strong>ggplot2 plot<\/strong> with neatly listed values bellow the plot. We used <strong>AirPassengers<\/strong> sample dataset in R and added \u201cK\u201d formatting for example\u2019s sake.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"669\" class=\"wp-image-1959\" src=\"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/word-image-21.png\" srcset=\"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/word-image-21.png 950w, https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/word-image-21-300x211.png 300w, https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/word-image-21-768x541.png 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/p>\n<p>To get the plot use the following R code.<\/p>\n<p><strong>dat = AirPassengers<\/strong><\/p>\n<p><strong>len = length(dat)<\/strong><\/p>\n<p><strong>df = data.frame(Date=as.Date(dat), AC=as.matrix(dat), PL = as.integer(as.matrix(dat)+runif(len, -15, 15), length=0))<\/strong><\/p>\n<p><strong>df = subset(df, Date &lt; as.Date(&#8220;1950-01-01&#8221;))<\/strong><\/p>\n<p><strong>df = melt(df, id.vars=&#8221;Date&#8221;)<\/strong><\/p>\n<p><strong>df$variable = factor(df$variable, levels = c(&#8220;PL&#8221;, &#8220;AC&#8221;))<\/strong><\/p>\n<p><strong>df = df[with(df, order(variable)),]<\/strong><\/p>\n<p><strong>df$textcolor = case_when(df$variable == &#8220;AC&#8221; ~ &#8220;white&#8221;,<\/strong><\/p>\n<p><strong> df$variable == &#8220;PL&#8221; ~ &#8220;black&#8221;,<\/strong><\/p>\n<p><strong> TRUE ~ &#8220;white&#8221;)<\/strong><\/p>\n<p><strong>df$labels = format(round(as.numeric(df$value), 1), nsmall=0, big.mark=&#8221;.&#8221;, decimal.mark = &#8220;,&#8221;)<\/strong><\/p>\n<p><strong>df$labels = paste(df$labels, &#8220;K&#8221;, sep=&#8221;&#8221;)<\/strong><\/p>\n<p><strong>textsize = 4.3<\/strong><\/p>\n<p><strong>g =<\/strong><\/p>\n<p><strong> ggplot(data = df, aes(x=Date, y=value, fill=variable)) +<\/strong><\/p>\n<p><strong> xlab(&#8220;&#8221;) +<\/strong><\/p>\n<p><strong> ylab(&#8220;&#8221;) +<\/strong><\/p>\n<p><strong> geom_bar(stat=&#8217;identity&#8217;, position = position_dodge(width = 8), width=50) +<\/strong><\/p>\n<p><strong> theme(legend.position=&#8221;top&#8221;,<\/strong><\/p>\n<p><strong> legend.justification=&#8217;left&#8217;,<\/strong><\/p>\n<p><strong> legend.text=element_text(size=text_size*1.2),<\/strong><\/p>\n<p><strong> panel.grid.major.y = element_line(colour=&#8221;azure2&#8243;, size=0.5),<\/strong><\/p>\n<p><strong> axis.text.y=element_text(size = text_size),<\/strong><\/p>\n<p><strong> axis.text.x=element_text(size = text_size),<\/strong><\/p>\n<p><strong> panel.background = element_rect(fill = &#8220;transparent&#8221;,colour = NA)<\/strong><\/p>\n<p><strong> ) +<\/strong><\/p>\n<p><strong> scale_x_date(date_labels=&#8221;%b \\n %Y&#8221;, breaks = df$Date,expand = c(0, 0)) +<\/strong><\/p>\n<p><strong> scale_y_continuous(labels = scales::number_format(accuracy = 1, decimal.mark = &#8216;.&#8217;, suffix = &#8220;K&#8221;), expand = c(0,0)) +<\/strong><\/p>\n<p><strong> scale_fill_manual(&#8220;&#8221;, values = c(&#8220;AC&#8221; = &#8220;black&#8221;, &#8220;PL&#8221; = &#8220;grey90&#8221;)) +<\/strong><\/p>\n<p><strong> geom_label(data=subset(df, variable==&#8221;AC&#8221;), aes(y=value\/2, label = labels), fill = &#8220;black&#8221;, colour = &#8220;white&#8221;, label.size = 0, size = textsize, nudge_x = 2, fontface = &#8220;bold&#8221;, vjust=&#8221;inward&#8221;)<\/strong><\/p>\n<p><strong>df$variable = factor(df$variable, levels = c(&#8220;PL&#8221;, &#8220;AC&#8221;))<\/strong><\/p>\n<p><strong>textsize= 4<\/strong><\/p>\n<p><strong>ggtable =<\/strong><\/p>\n<p><strong> ggplot(df, aes(x=Date, y=variable, fill=variable)) +<\/strong><\/p>\n<p><strong> xlab(&#8220;&#8221;) +<\/strong><\/p>\n<p><strong> ylab(&#8220;&#8221;) +<\/strong><\/p>\n<p><strong> theme(legend.position=&#8221;none&#8221;,<\/strong><\/p>\n<p><strong> panel.grid.major.x = element_blank(),<\/strong><\/p>\n<p><strong> panel.grid.major.y = element_blank(),<\/strong><\/p>\n<p><strong> panel.grid.minor.x = element_blank(),<\/strong><\/p>\n<p><strong> panel.grid.minor.y = element_blank(),<\/strong><\/p>\n<p><strong> axis.text.y=element_text(size = 12),<\/strong><\/p>\n<p><strong> axis.text.x=element_blank(),<\/strong><\/p>\n<p><strong> panel.ontop = TRUE,<\/strong><\/p>\n<p><strong> axis.ticks.x=element_blank(),<\/strong><\/p>\n<p><strong> panel.background = element_rect(fill = &#8220;transparent&#8221;,colour = NA)<\/strong><\/p>\n<p><strong> ) +<\/strong><\/p>\n<p><strong> scale_x_date(date_labels=&#8221;%b \\n %Y&#8221;, breaks = df$Date) +<\/strong><\/p>\n<p><strong> scale_fill_manual(&#8220;&#8221;, values = c(&#8220;AC&#8221; = &#8220;black&#8221;, &#8220;PL&#8221; = &#8220;grey90&#8221;)) +<\/strong><\/p>\n<p><strong> scale_y_discrete(expand = c(0, 0)) +<\/strong><\/p>\n<p><strong> geom_rect(xmin=-Inf, xmax = Inf, aes(ymin = as.numeric(variable)-0.5, <\/strong><\/p>\n<p><strong> ymax = as.numeric(variable)+0.5, fill = variable), alpha = 0.1) +<\/strong><\/p>\n<p><strong> geom_text(aes(y=variable, color = textcolor, label = ifelse(value == &#8220;&lt;NA&gt;&#8221;, &#8220;&#8221;, labels)), size = textsize, fontface = &#8220;bold&#8221;) +<\/strong><\/p>\n<p><strong> scale_color_manual(&#8220;&#8221;, values = c(&#8220;black&#8221; = &#8220;black&#8221;, &#8220;white&#8221; = &#8220;white&#8221;, &#8220;grey35&#8243;= &#8220;grey50&#8221;, &#8220;grey90&#8243;=&#8221;grey90&#8221;, &#8220;green&#8221;=&#8221;palegreen3&#8221;, &#8220;red&#8221;=&#8221;indianred2&#8221;)) <\/strong><\/p>\n<p><strong>theme_set(theme_grey())<\/strong><\/p>\n<p><strong>plot_grid(g, ggtable, ncol = 1, align = &#8220;v&#8221;, rel_heights = c(5, 1))<\/strong><\/p>\n<p>Tweak values of parameters to make the plot look good with dimensions you need. Happy plotting!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When plotting charts, we often want to see the actual values listed as a table. Bellow we created ggplot2 plot with neatly listed values bellow the plot. We used AirPassengers sample dataset in R and added \u201cK\u201d formatting for example\u2019s<\/p>\n","protected":false},"author":5,"featured_media":1960,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>ggplot2 With Table of Values - Bi Unija<\/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:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ggplot2 With Table of Values - Bi Unija\" \/>\n<meta property=\"og:description\" content=\"When plotting charts, we often want to see the actual values listed as a table. Bellow we created ggplot2 plot with neatly listed values bellow the plot. We used AirPassengers sample dataset in R and added \u201cK\u201d formatting for example\u2019s\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/\" \/>\n<meta property=\"og:site_name\" content=\"Bi Unija\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-24T07:28:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-24T07:28:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png\" \/>\n\t<meta property=\"og:image:width\" content=\"720\" \/>\n\t<meta property=\"og:image:height\" content=\"480\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Branka Trifunovi\u0107\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Branka Trifunovi\u0107\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/\",\"url\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/\",\"name\":\"ggplot2 With Table of Values - Bi Unija\",\"isPartOf\":{\"@id\":\"https:\/\/bi.unija.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png\",\"datePublished\":\"2021-05-24T07:28:16+00:00\",\"dateModified\":\"2021-05-24T07:28:40+00:00\",\"author\":{\"@id\":\"https:\/\/bi.unija.com\/en\/#\/schema\/person\/3bed36623727c7162e421c4366a54e10\"},\"breadcrumb\":{\"@id\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#primaryimage\",\"url\":\"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png\",\"contentUrl\":\"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png\",\"width\":720,\"height\":480},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/bi.unija.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ggplot2 With Table of Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/bi.unija.com\/en\/#website\",\"url\":\"https:\/\/bi.unija.com\/en\/\",\"name\":\"Bi Unija\",\"description\":\"Just another Bi site\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/bi.unija.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/bi.unija.com\/en\/#\/schema\/person\/3bed36623727c7162e421c4366a54e10\",\"name\":\"Branka Trifunovi\u0107\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/bi.unija.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec99f651bcc98a163161515ec254b974?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec99f651bcc98a163161515ec254b974?s=96&d=mm&r=g\",\"caption\":\"Branka Trifunovi\u0107\"},\"url\":\"https:\/\/bi.unija.com\/en\/author\/branka\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ggplot2 With Table of Values - Bi Unija","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:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/","og_locale":"en_US","og_type":"article","og_title":"ggplot2 With Table of Values - Bi Unija","og_description":"When plotting charts, we often want to see the actual values listed as a table. Bellow we created ggplot2 plot with neatly listed values bellow the plot. We used AirPassengers sample dataset in R and added \u201cK\u201d formatting for example\u2019s","og_url":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/","og_site_name":"Bi Unija","article_published_time":"2021-05-24T07:28:16+00:00","article_modified_time":"2021-05-24T07:28:40+00:00","og_image":[{"width":720,"height":480,"url":"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png","type":"image\/png"}],"author":"Branka Trifunovi\u0107","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Branka Trifunovi\u0107","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/","url":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/","name":"ggplot2 With Table of Values - Bi Unija","isPartOf":{"@id":"https:\/\/bi.unija.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#primaryimage"},"image":{"@id":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#primaryimage"},"thumbnailUrl":"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png","datePublished":"2021-05-24T07:28:16+00:00","dateModified":"2021-05-24T07:28:40+00:00","author":{"@id":"https:\/\/bi.unija.com\/en\/#\/schema\/person\/3bed36623727c7162e421c4366a54e10"},"breadcrumb":{"@id":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#primaryimage","url":"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png","contentUrl":"https:\/\/bi.unija.com\/en\/wp-content\/uploads\/sites\/2\/2021\/05\/48.png","width":720,"height":480},{"@type":"BreadcrumbList","@id":"https:\/\/bi.unija.com\/en\/ggplot2-with-table-of-values\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/bi.unija.com\/en\/"},{"@type":"ListItem","position":2,"name":"ggplot2 With Table of Values"}]},{"@type":"WebSite","@id":"https:\/\/bi.unija.com\/en\/#website","url":"https:\/\/bi.unija.com\/en\/","name":"Bi Unija","description":"Just another Bi site","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/bi.unija.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/bi.unija.com\/en\/#\/schema\/person\/3bed36623727c7162e421c4366a54e10","name":"Branka Trifunovi\u0107","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bi.unija.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec99f651bcc98a163161515ec254b974?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec99f651bcc98a163161515ec254b974?s=96&d=mm&r=g","caption":"Branka Trifunovi\u0107"},"url":"https:\/\/bi.unija.com\/en\/author\/branka\/"}]}},"_links":{"self":[{"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/posts\/1958"}],"collection":[{"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/comments?post=1958"}],"version-history":[{"count":2,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/posts\/1958\/revisions"}],"predecessor-version":[{"id":1962,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/posts\/1958\/revisions\/1962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/media\/1960"}],"wp:attachment":[{"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/media?parent=1958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/categories?post=1958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bi.unija.com\/en\/wp-json\/wp\/v2\/tags?post=1958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}