{"id":2426,"date":"2018-06-17T08:39:24","date_gmt":"2018-06-17T07:39:24","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=2426"},"modified":"2025-06-23T18:23:40","modified_gmt":"2025-06-23T17:23:40","slug":"grammar-of-graphics","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/grammar-of-graphics\/","title":{"rendered":"Grammar of Graphics"},"content":{"rendered":"\n<p>Similar to language, graphics are subject to rules. This&nbsp;was called the &#8216;grammar of graphics&#8217; by Leland Wilkinson and further described in Hadley Wickham&#8217;s book<sup class='sup-ref-note' id='note-zotero-ref-p2426-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p2426-r1'>1<\/a><\/sup>. The grammar of graphics is applied in the ggplot2 package<sup class='sup-ref-note' id='note-zotero-ref-p2426-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p2426-r2'>2<\/a><\/sup>&nbsp;that is part of the <a href=\"https:\/\/ggplot2.tidyverse.org\/\" data-type=\"link\" data-id=\"https:\/\/ggplot2.tidyverse.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">tidyverse family of packages<\/a>.<\/p>\n\n\n\n<p>The&nbsp;ggplot2 package<sup class='sup-ref-note' id='note-zotero-ref-p2426-r3-o1'><a class='sup-ref-note' href='#zotero-ref-p2426-r3'>3<\/a><\/sup> is very versatile and allows the creation of high quality plots. This section gives the basis on how to create a plot using the grammar of graphics. Further information is available in the&nbsp;comprehensive package reference manual and Winston Chang&#8217;s R Graphics Cookbook&nbsp;<sup class='sup-ref-note' id='note-zotero-ref-p2426-r4-o1'><a class='sup-ref-note' href='#zotero-ref-p2426-r4'>4<\/a><\/sup> (that is also <a href=\"http:\/\/www.cookbook-r.com\/Graphs\/\" data-type=\"link\" data-id=\"http:\/\/www.cookbook-r.com\/Graphs\/\" target=\"_blank\" rel=\"noreferrer noopener\">online<\/a>. In addition <a href=\"https:\/\/stackoverflow.com\/\" target=\"_blank\" rel=\"noopener\">Stack Overflow <\/a>is a useful source of information.<\/p>\n\n\n\n<p>Further examples of different plots and how to create them is also described in the&nbsp;<a href=\"https:\/\/pcool.dyndns.org\/index.php\/plots\/\" data-type=\"page\" data-id=\"358\">plots section<\/a>.<\/p>\n\n\n\n<p>For the examples below, the ggplot2 package<sup class='sup-ref-note' id='note-zotero-ref-p2426-r5-o1'><a class='sup-ref-note' href='#zotero-ref-p2426-r5'>5<\/a><\/sup> should be <a href=\"https:\/\/pcool.dyndns.org\/index.php\/packages\/\" data-type=\"page\" data-id=\"22\">installed<\/a> and loaded:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(ggplot2)<\/em><\/span><\/code><\/pre>\n\n\n\n<p>The package contains sample data of 53,940 diamonds that can be viewed:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">diamonds<\/span><\/em>\n<em><span style=\"color: #ff0000;\">diamonds<\/span><\/em>\n<span style=\"color: #0000ff;\"><em># A tibble: 53,940 x 10<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> carat cut color clarity depth table price x y z<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> &lt;dbl&gt; &lt;ord&gt; &lt;ord&gt; &lt;ord&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49<\/em><\/span>\n<span style=\"color: #0000ff;\"><em>10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39<\/em><\/span>\n<span style=\"color: #0000ff;\"><em># ... with 53,930 more rows<\/em><\/span><\/code><\/pre>\n\n\n\n<p>The diamond data frame is used in the examples described below.<\/p>\n\n\n\n<p><strong>Basic elements<\/strong><\/p>\n\n\n\n<p>Every plot made with the ggplot package should contain a minimum of <strong>three elements<\/strong>. These are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Source data frame<\/strong>\n<ul class=\"wp-block-list\">\n<li>This is the name of the data frame used for the plot<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Aesthetic elements<\/strong>\n<ul class=\"wp-block-list\">\n<li>These are the variables to be displayed in the plot. Variables can be bound to axes (x or y), but also colours, shapes, colour intensity and others.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Geometric element<\/strong>\n<ul class=\"wp-block-list\">\n<li>This is the type of plot that should be displayed (bar chart, histogram, scatter plot, box and whisker plot etc). There are many different geometric elements&nbsp;included in the package and these are described in the manual. Examples can also be found&nbsp;in the&nbsp;<a href=\"https:\/\/pcool.dyndns.org\/index.php\/plots\/\" data-type=\"page\" data-id=\"358\">plots section<\/a>.&nbsp; In addition, it is possible to define your own geometric element, but this is outside the scope of this section.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>A plot is created by calling the ggplot() function and adding elements or layers to it using the &#8216;+&#8217; sign. This can be done as one long call, or by saving the first element as an object and subsequently adding more layers to it. Both methods will be shown here (in the single dimensional plot below), but for brevity, the one long call will be used in the remainder of this section.<\/p>\n\n\n\n<p><strong>Single dimensional plot<\/strong><\/p>\n\n\n\n<p>A good example of a single dimensional plot is a histogram (for continuous data). To create a histogram (geometric element) of the carat variable (aesthetic element) in the diamond data frame (source):<\/p>\n\n\n\n<p><em><strong>Single command method:<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">geom_histogram()<\/span><\/em><\/code><\/pre>\n\n\n\n<p>Will create the following plot with default bin width (this can be altered by changing the bin width or the number of bins allowed with the geometric element):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"911\" height=\"910\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat.png\" alt=\"\" class=\"wp-image-2935\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat.png 911w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat-768x767.png 768w\" sizes=\"auto, (max-width: 911px) 100vw, 911px\" \/><\/figure>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\">Please note that, when commands are entered in the console, the different options and their default values are displayed by R.<\/p>\n\n\n\n<p><em><strong>Additive method:<\/strong><\/em><\/p>\n\n\n\n<p>The same plot can be created by saving the first command as an R object and subsequently adding to it. This method can be useful if you are building plot a plot step by step:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>my_plot &lt;- ggplot(diamonds, aes(x = carat))<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>my_plot &lt;- my_plot + geom_histogram()<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>my_plot<\/em><\/span><\/code><\/pre>\n\n\n\n<p>This will create the same plot. For brevity, the single long command line method will be used in the remainder of this section.<\/p>\n\n\n\n<p>Each geometric element has different settings. Default settings are set automatically, but these can be altered within the geometric element call (please refer to the reference manual for a full description of each geometric element). For example, to&nbsp;reduce the numbers of bins to 10&nbsp; (bins = 10), the outline colour to black (colour = &#8220;black&#8221;) and fill colour to red (fill = &#8220;red&#8221;):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">geom_histogram(bins = 10, colour = \"black\",<\/span><\/em> <em><span style=\"color: #ff0000;\">fill = \"red\")<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"910\" height=\"912\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_red.png\" alt=\"\" class=\"wp-image-2931\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_red.png 910w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_red-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_red-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_red-768x770.png 768w\" sizes=\"auto, (max-width: 910px) 100vw, 910px\" \/><\/figure>\n\n\n\n<p><strong>Multi dimensional plot<\/strong><\/p>\n\n\n\n<p>A scatter plot has two dimensions, one for the x axis and one for the y axis. Using the same rules, it is easy to create a scatter plot with the carat variable on the x axis and the price variable on the y axis:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>ggplot(diamonds, aes(x = carat, y = price)) + <\/em><\/span>\n  <span style=\"color: #ff0000;\"><em>geom_point()<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"913\" height=\"913\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price1.png\" alt=\"\" class=\"wp-image-2927\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price1.png 913w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price1-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price1-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price1-768x768.png 768w\" sizes=\"auto, (max-width: 913px) 100vw, 913px\" \/><\/figure>\n\n\n\n<p>It is also straight forward to add further dimensions by <em><strong>adding these to the aesthetic element<\/strong><\/em>. For example, the categorical variable clarity can be mapped to the colour of the points to demonstrate the influence of clarity on price:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat, y = price, colour = clarity)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">geom_point()<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"915\" height=\"914\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity.png\" alt=\"\" class=\"wp-image-2902\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity.png 915w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity-768x767.png 768w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><\/figure>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--2\">Please note a legend is automatically added.<\/p>\n\n\n\n<p>In addition,&nbsp;the shape of the point can be mapped to the categorical variable cut:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat, y = price, colour = clarity, <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">shape = cut)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_point()<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"911\" height=\"912\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut.png\" alt=\"\" class=\"wp-image-2898\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut.png 911w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut-768x769.png 768w\" sizes=\"auto, (max-width: 911px) 100vw, 911px\" \/><\/figure>\n\n\n\n<p>The&nbsp;color variable can be mapped to the transparency (alpha)&nbsp;to create a transparency scale:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat, y = price, colour = clarity, <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">shape = cut, alpha = color)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_point()<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"911\" height=\"909\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_color.png\" alt=\"\" class=\"wp-image-2890\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_color.png 911w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_color-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_color-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_color-768x766.png 768w\" sizes=\"auto, (max-width: 911px) 100vw, 911px\" \/><\/figure>\n\n\n\n<p>It is also possible to map continuous variables to an aesthetic element. For example map the to depth to the size of the points in the scatter plot:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat, y = price, colour = clarity, <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">shape = cut, alpha = color, size = depth)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_point()<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"987\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_depth.png\" alt=\"\" class=\"wp-image-2894\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_depth.png 984w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_depth-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_depth-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_clarity_cut_depth-768x770.png 768w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><\/figure>\n\n\n\n<p>Although it is possible to add all these dimension to a plot, it makes it difficult to interpret and this is discouraged. <em><strong>It is better to keep the plots simple!<\/strong><\/em><\/p>\n\n\n\n<p><strong>Combining variables in a single plot<\/strong><\/p>\n\n\n\n<p>It is common practice to declare the source data frame and aesthetics in the ggplot() function call, but this is not essential. Data source and variables can also be declared within the geometric element. For example, the histogram above could also be created by entering:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot() + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_histogram(data = diamonds, aes(x = carat), bins = 10, <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">colour = \"black\", fill = \"red\")<\/span><\/em><\/code><\/pre>\n\n\n\n<p>This instruction gives an identical plot to:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_histogram(bins = 10, colour = \"black\", <\/span><\/em><em><span style=\"color: #ff0000;\">fill = \"red\")<\/span><\/em><\/code><\/pre>\n\n\n\n<p>Declaring the source and aesthetic elements within the geometric element&nbsp;will allow the addition of different data sources within the same plot. For example, to create a histogram of the carat variable and price variable in the same plot:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot() + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_histogram(data = diamonds, aes(x = carat), colour = \"black\", <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">fill = \"red\") + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_histogram(data = diamonds, aes(x = price), colour = \"black\", <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">fill = \"green\")<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"490\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price-1024x490.png\" alt=\"\" class=\"wp-image-2923\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price-1024x490.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price-300x144.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price-768x368.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price.png 1349w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As can be seen, the range of the price variable is considerably larger than the&nbsp;range of the carat variable. Automatic axis scaling displays all data. Consequently, there is only one bin for the carat variable. Furthermore, there are no free diamonds and the price variable has no bin at zero. To display both&nbsp;histograms&nbsp;in the same plot, the carat variable can be scaled to similar magnitude as the price variable (multiplied by 10,000):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>ggplot() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_histogram(data = diamonds, aes(x = carat * 10000), <\/em><\/span>\n  <span style=\"color: #ff0000;\"><em>colour = \"black\", fill = \"red\", alpha = 0.5) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_histogram(data = diamonds, aes(x = price), colour = \"black\", <\/em><\/span>\n  <span style=\"color: #ff0000;\"><em>fill = \"green\", alpha = 0.5)<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"494\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat10000_price-1024x494.png\" alt=\"\" class=\"wp-image-2940\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat10000_price-1024x494.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat10000_price-300x145.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat10000_price-768x371.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat10000_price.png 1345w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--3\">Please note the transparency has been set to 50% (alpha = 0.5) to display overlapping histograms.<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--4\">Please note the x axis label displays the name of the variable in the first histogram create (carat*10000). The label on the x axis can be changed by adding an axis layer (see below).<\/p>\n\n\n\n<p>Further examples of different plots and how to create them is described in the <a href=\"https:\/\/pcool.dyndns.org\/index.php\/plots\/\" data-type=\"page\" data-id=\"358\">plots section<\/a>.<\/p>\n\n\n\n<p><strong>Adding layers<\/strong><\/p>\n\n\n\n<p>Once the basic plot is displayed, layers can be added to&nbsp;add a title or change an axis. It is impossible to name all the possibilities, but please refer to the reference manual for further information. Going back to the histogram created above, it is easy to add a title:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">  geom_histogram(bins = 10, colour = \"black\", fill = \"red\") + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">ggtitle(\"Histogram of Diamond Size\")<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"980\" height=\"980\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_title.png\" alt=\"\" class=\"wp-image-2910\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_title.png 980w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_title-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_title-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_title-768x768.png 768w\" sizes=\"auto, (max-width: 980px) 100vw, 980px\" \/><\/figure>\n\n\n\n<p>and change the x axis:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fd0101\" class=\"has-inline-color\">ggplot(diamonds, aes(x = carat)) + <\/mark><\/em>\n  <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fd0101\" class=\"has-inline-color\">geom_histogram(bins = 10, colour = \"black\", fill = \"red\") +        <\/mark><\/em>\n  <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fd0101\" class=\"has-inline-color\">ggtitle(\"Histogram of Diamond Size\") + <\/mark><\/em>\n  <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fd0101\" class=\"has-inline-color\">scale_x_continuous(\"Size in Carats\", limits = c(0, 6))<\/mark><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"979\" height=\"981\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_x_axis.png\" alt=\"\" class=\"wp-image-2914\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_x_axis.png 979w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_x_axis-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_x_axis-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_x_axis-768x770.png 768w\" sizes=\"auto, (max-width: 979px) 100vw, 979px\" \/><\/figure>\n\n\n\n<p>and the y axis:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">geom_histogram(bins = 10, colour = \"black\", fill = \"red\") + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">ggtitle(\"Histogram of Diamond Size\") + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">scale_x_continuous(\"Size in Carats\", limits = c(0, 6)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">scale_y_continuous(\"Number of Diamonds\", limits = c(0, 30000))<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"982\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_y_axis.png\" alt=\"\" class=\"wp-image-2918\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_y_axis.png 981w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_y_axis-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_y_axis-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_y_axis-768x769.png 768w\" sizes=\"auto, (max-width: 981px) 100vw, 981px\" \/><\/figure>\n\n\n\n<p><strong>Themes<\/strong><\/p>\n\n\n\n<p>The ggplot package also contains themes to allow the creation of different plots with the same lay out. For example, the black and white theme can be used to create plots suitable for publication:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot(diamonds, aes(x = carat)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">geom_histogram(bins = 10, colour = \"black\", fill = \"red\") +   <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">ggtitle(\"Histogram of Diamond Size\") + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">scale_x_continuous(\"Size in Carats\", limits = c(0, 6)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">scale_y_continuous(\"Number of Diamonds\", limits = c(0, 30000)) + <\/span><\/em>\n  <em><span style=\"color: #ff0000;\">theme_bw(base_size = 14, base_family = \"Arial\")<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"983\" height=\"978\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_theme.png\" alt=\"\" class=\"wp-image-2906\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_theme.png 983w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_theme-300x298.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_theme-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/carat_price_theme-768x764.png 768w\" sizes=\"auto, (max-width: 983px) 100vw, 983px\" \/><\/figure>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--5\">Please note the font type and font size can be set in the theme call.<\/p>\n\n\n\n<p>The plots created above have automatically the&nbsp;default ggplot theme applied to them. Although the plots look good, they are instantly recognisable as being created by the ggplot package. Authors may want to create their own theme.&nbsp; This is described in the <a href=\"https:\/\/pcool.dyndns.org\/index.php\/importing-fonts-setting-themes-and-defining-colours\/\" data-type=\"page\" data-id=\"2097\">themes section<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Similar to language, graphics are subject to rules. This&nbsp;was called the &#8216;grammar of graphics&#8217; by Leland Wilkinson and further described in Hadley Wickham&#8217;s book. The grammar of graphics is applied in the ggplot2 package&nbsp;that is part of the tidyverse family of packages. The&nbsp;ggplot2 package is very versatile and allows the creation of high quality plots. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-2426","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=2426"}],"version-history":[{"count":7,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2426\/revisions"}],"predecessor-version":[{"id":4308,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2426\/revisions\/4308"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=2426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}