{"id":175266,"date":"2023-12-17T10:05:29","date_gmt":"2023-12-17T09:05:29","guid":{"rendered":"https:\/\/liora.io\/en\/?p=175266"},"modified":"2026-02-06T08:40:44","modified_gmt":"2026-02-06T07:40:44","slug":"ggplot-everything-you-need-to-know-about-the-r-data-visualization-library","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/ggplot-everything-you-need-to-know-about-the-r-data-visualization-library","title":{"rendered":"ggplot : Everything you need to know about the R data visualization library"},"content":{"rendered":"<h2>In this article, we will explore the fundamental concepts of ggplot and learn how to create a chart using this library to effectively present your data.<\/h2>\t\t\n\t\t\t<h3>What is ggplot?<\/h3>\t\t\n\t\t<p><strong>ggplot<\/strong> is a<a href=\"https:\/\/liora.io\/en\/google-data-studio-introduction-to-googles-dataviz-tool\"> data visualization library<\/a> in R, developed by Hadley Wickham in 2005. This library is based on the grammar of graphics, which allows describing graphs in terms of basic components such as axes, legends, or labels.<\/p><p>With ggplot, you can think of a graph as a series of layers that stack up to produce the final graph. Each graph layer can be added using the + function and can include elements such as points, lines, bars, scatterplots, histograms, boxplots, text, and much more.<\/p><p>To create a graph using the layering system in ggplot, start by specifying the data and the variables to use for the x and y axes, then gradually add additional graph layers.<\/p><p>One of the most commonly used graph layers involves geometric functions using the appropriate geom_ functions.<\/p><p>Here are some examples of geometric function graph layers:<\/p><p>&#8211; geom_point(): adds points to the graph<br>&#8211; geom_line(): adds a line to the graph<br>&#8211; geom_bar(): adds a bar chart to the graph<br>&#8211; geom_histogram(): adds a histogram to the graph<br>&#8211; geom_boxplot(): adds a boxplot to the graph<br>&#8211; geom_text(): adds text to the graph<\/p><p>Please note that each graph layer can be <strong>customized using function-specific options.<\/strong><\/p><p>To understand this principle, let&#8217;s take a step-by-step look at how to create the following chart with the Iris dataset.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image6.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>Here is an overview of our data:<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image4-1.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-analyst\">Learn to use ggplot<\/a><\/div><\/div>\n\n\t\t\t<h4>Step 1: Load the ggplot library and read the csv file <\/h4>\t\t\n\t\t<p>library(ggplot2)<\/p><p>iris &lt;- read.csv(&#8220;species.csv&#8221;)<\/p>\t\t\n\t\t\t<h4>Step 2: Create the ggplot object<\/h4>\t\t\n\t\t<pre>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))<\/pre>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image5.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>This <strong>line creates a ggplot<\/strong> object named p, which represents the iris dataset with the variables Sepal.Length, Petal.Length, Sepal.Width and Petal.Width. The values of Sepal.Length and Petal.Length are added to create the x-axis, while the values of Sepal.Width and Petal.Width are added to create the y-axis.<\/p>\t\t\n\t\t\t<h4>Step 3: Creating a point cloud<\/h4>\t\t\n\t\t<pre>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))&nbsp;<\/pre><p>+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image3-1.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>This line adds a point cloud (geom_jitter) to the graph. The points are colored according to the Species variable and have a transparency of 0.6 and a width of 1.<\/p>\t\t\n\t\t\t<h4>Step 4: Create a linear regression<\/h4>\t\t\n\t\t<pre>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))&nbsp;<br><br>+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)<br><br>+ geom_smooth(method='lm', se = FALSE)<\/pre>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image7.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>This<a href=\"https:\/\/liora.io\/en\/classification-algorithms-definition-and-main-models\"> line adds a regression line layer<\/a> (geom_smooth) to the graph. The modeling method used is linear regression (method=&#8217;lm&#8217;). The se = FALSE option is used to avoid displaying confidence intervals.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-analyst\">Learn to master ggplot with Liora<\/a><\/div><\/div>\n\n\t\t\t<h4>Step 5: Separate the graph into sub-sections<\/h4>\t\t\n\t\t<pre>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))<br><br>+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)<br><br>+ geom_smooth(method='lm', se = FALSE)&nbsp;<br><br>+ facet_wrap(~Species)<\/pre>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image2-1.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>This line divides the graph into panels (facet_wrap) according to the Species variable. This allows you to see the relationship between variables for each species separately.<\/p>\t\t\n\t\t\t<h4>Step 6: Add labels<\/h4>\t\t\n\t\t<pre>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width)) + geom_jitter(aes(color = Species), alpha =0.6, width = 1)<br><br>+ geom_smooth(method='lm', se = FALSE)&nbsp;<br><br>+ facet_wrap(~Species)&nbsp;<br><br>+ labs(title = \"Relation Length\/Width\", x= \"Length\", y= \"Width\")<\/pre>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image1.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>This line adds title and axis labels to the graph. The title is &#8220;Relation Length\/Width&#8221;, the x-axis is labelled &#8220;Length&#8221; and the y-axis is labelled &#8220;Width&#8221;.<\/p>\t\t\n\t\t\t<h4>Step 7: Adding the theme<\/h4>\t\t\n\t\t<pre>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))&nbsp;<br><br>+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)<br><br>+ geom_smooth(method='lm', se = FALSE)&nbsp;<br><br>+ facet_wrap(~Species)&nbsp;<br><br>+ labs(title = \"Relation Length\/Width\", x= \"Length\", y= \"Width\")<br><br>+&nbsp; theme(plot.background = element_rect(fill = '#E8EAF6', color = \"#08104E\", size = 3))<\/pre>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image6-1.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>This <strong>line defines a custom theme<\/strong> for the graphic using the theme() function. The argument plot.background is used to define the background of the graphic. The element_rect() function is used to create a rectangle with a fill color of &#8216;#E8EAF6&#8217;, a border color of &#8220;#08104E&#8221; and a thickness of 3 pixels.<\/p><p>This code creates a 7-step ggplot graph showing the relationship between sepal and petal length and width for different iris flower species. Points are colored according to species, and a linear regression is fitted for each species.<\/p>\t\t\n\t\t\t<h3>What do I need to know about ggplot?<\/h3>\t\t\n\t\t<p>ggplot is a library for<a href=\"https:\/\/liora.io\/en\/altair-everything-you-need-to-know-about-this-statistical-visualization-library\"> data visualization<\/a> in R. Thanks to its flexible layering system, we can create complex custom graphics by gradually adding additional graphic components.<\/p><p>If you&#8217;re interested in data visualization, don&#8217;t hesitate to join our Data Analyst training course!<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-analyst\">Register for our Data Analyst training<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore the fundamental concepts of ggplot and learn how to create a chart using this library to effectively present your data. What is ggplot? ggplot is a data visualization library in R, developed by Hadley Wickham in 2005. This library is based on the grammar of graphics, which allows describing [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":175273,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-175266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=175266"}],"version-history":[{"count":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175266\/revisions"}],"predecessor-version":[{"id":206165,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175266\/revisions\/206165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/175273"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=175266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=175266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}