<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/coordinate-systems-in-ggplot2-easily-overlooked-and-rather-underrated/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.statworx.com/de/blog/coordinate-systems-in-ggplot2-easily-overlooked-and-rather-underrated/"> r-bloggers – STATWORX</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>All plots have coordinate systems. Perhaps because they are such an integral element of plots, they are easily overlooked. However, in ggplot2, there are several very useful options to customize the coordinate systems of plots, which we will not overlook but explore in this blog post. </p>
<p>Since it is spring, we will use a random subset of the famous iris data set. When we plot the petal length against the petal width, and map species onto color and play around a little with the shape, color and sizes of aesthetics, one obtains this vernal plot:</p>
<p><img alt="plot base iris data" src="https://i1.wp.com/www.statworx.com/wp-content/uploads/plot-base-iris-data.jpg?w=450&ssl=1" class="wp-image-13894" data-recalc-dims="1"></p>
<pre><code lang="python"># Base plot
plot_base &lt;- ggplot(data = df_iris) +
  geom_point(aes(x = Petal.Length, y = Petal.Width, color = Species), 
             size = 3, alpha = 0.9, shape = 8) +
  geom_point(aes(x = Petal.Length, y = Petal.Width), 
             color = &quot;yellow&quot;, size = 0.4) +
  scale_color_manual(values = c(&quot;#693FE9&quot;, &quot;#A089F8&quot;, &quot;#0000FF&quot;)) +
  theme_minimal()
</code></pre>
<h2>Cartesian coordinate system</h2>
<h3>Zooming in and out</h3>
<p>The coordinate system can be manipulated by adding one of ggplot’s different coordinate systems. When you are imagining a coordinate system, you are most likely thinking of a Cartesian one. The Cartesian coordinate system combines x and y dimension orthogonally and is ggplots default (<em>coord_cartesian</em>).</p>
<p>There also are several varaitions of the familiar Cartesian coordinate system in ggplot, namely <em>coord_fixed</em>, <em>coord_flip</em> and <em>coord_trans</em>. For all of them, the displayed section of the data can be specified by defining the maximal value depicted on the x (<em>xlim =</em>) and y (<em>ylim =</em>) axis. This allows to “zoom in” or “zoom out” of a plot. It is a great advantage, that all manipulations of the coordinate system only alter the depiction of the data but not the data itself.</p>
<pre><code># Zooming in with xlim/ylim
plot_base +
  coord_cartesian(xlim = 5, ylim = 2) +
  ggtitle(&quot;coord_cartesian with xlim = 5 and ylim = 2&quot;)
</code></pre>
<p><img alt="zoomed scatter plot" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/zoomed-in-plot.jpg?w=450&ssl=1" class="wp-image-13903" data-recalc-dims="1"></p>
<h3>Specifying the “aspect ratio” of the axes</h3>
<p>Via <em>coord_fixed</em> one can specify the exact ratio of the length of a y unit relative to the length of a x unit within the final visualization. </p>
<pre><code># Setting the &quot;aspect ratio&quot; of y vs. x units
plot_base + 
  coord_fixed(ratio = 1/2) +
  ggtitle(&quot;coord_fixed with ratio = 1/2&quot;)
</code></pre>
<p><img alt="plot aspect ratio" src="https://i1.wp.com/www.statworx.com/wp-content/uploads/plot-aspect-ratio.jpg?w=450&ssl=1" class="wp-image-13891" data-recalc-dims="1"></p>
<h3>Transforming the scales of the axes</h3>
<p>This helps to emphasize the exact insight one wants to communicate. Another way to do so is <em>coord_trans</em>, which allows several transformations of the x and y variable (see table below, taken from Wickham 2016 page 97). Let me stress this again, very conveniently such transformations only pertain to the depicted – not the actual – scale of the data. This also is the reason why, regardless of the conducted transformation, the original values are used as axis labels. </p>
<figure>
<table>
<thead>
<tr>
<th>Name</th>
<th>Funktion <img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-a7ee323bc5a3f73ad5e066b13bed5504_l3.png?resize=34%2C18&ssl=1" alt="f(x)" width="34" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></th>
<th>Inverse <img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-64223eeb897fa0f0d10fc772ea7f209a_l3.png?resize=51%2C19&ssl=1" alt="f^{ -1 } (y)" width="51" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="19"></th>
</tr>
</thead>
<tbody>
<tr>
<td>asn</td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-216a309795917501614b8f66c1f429b5_l3.png?resize=78%2C19&ssl=1" alt="tanh^{-1}(x)" width="78" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="19"></td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-5ffaf337c2bbe22698de145795eb7a0f_l3.png?resize=77%2C19&ssl=1" alt="tanh^{-1}(y)" width="77" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="19"></td>
</tr>
<tr>
<td>exp</td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-34e96bdd13dc6d8d98abb2717b4e8bf3_l3.png?resize=16%2C12&ssl=1" alt="e^x" width="16" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="12"></td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-a9ee37a51088603c12e45d1d39862793_l3.png?resize=46%2C18&ssl=1" alt="log(y)" width="46" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></td>
</tr>
<tr>
<td>identity</td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-ede05c264bba0eda080918aaa09c4658_l3.png?resize=10%2C8&ssl=1" alt="x" width="10" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="8"></td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-0af556714940c351c933bba8cf840796_l3.png?resize=9%2C12&ssl=1" alt="y" width="9" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="12"></td>
</tr>
<tr>
<td>log</td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-2e1bc7f1882e5bec029c5d7a550ad963_l3.png?resize=46%2C18&ssl=1" alt="log(x)" width="46" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-19f3b6ba125bda146128ab41b036472c_l3.png?resize=15%2C12&ssl=1" alt="e^y" width="15" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="12"></td>
</tr>
<tr>
<td>log10</td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-d6b8da050fe34d7038f38ee77178bffc_l3.png?resize=60%2C18&ssl=1" alt="log_{10}(x)" width="60" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-8c1859bfef136c58521de92acbe63269_l3.png?resize=24%2C13&ssl=1" alt="10^y" width="24" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="13"></td>
</tr>
<tr>
<td>log2</td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-b7a5e8f11e453fc9b66c7b8f60ecef63_l3.png?resize=53%2C18&ssl=1" alt="log_{2}(x)" width="53" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-da4e31cd88334beb0ab6675167ece718_l3.png?resize=16%2C12&ssl=1" alt="2^y" width="16" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="12"></td>
</tr>
<tr>
<td>logit</td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-8bff23f7b2ada6e068a3c31ca753b8f6_l3.png?resize=65%2C21&ssl=1" alt="log(frac{ x }{ 1-x })" width="65" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="21"></td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-70c3d5c9f695bc3906b024f0655a391d_l3.png?resize=82%2C23&ssl=1" alt="log(frac{ x }{ 1+e(y) })" width="82" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="23"></td>
</tr>
<tr>
<td>pow10</td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-77f326ae98fc6bf11e2b356d1b6ad08f_l3.png?resize=25%2C13&ssl=1" alt="10^x" width="25" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="13"></td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-ce2a0b1173e1e15e422dec0442ec753e_l3.png?resize=59%2C18&ssl=1" alt="log_{10}(y)" width="59" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></td>
</tr>
<tr>
<td>probit</td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-3bc378a59974180f8935e9e32ae1426c_l3.png?resize=36%2C18&ssl=1" alt="Phi(x)" width="36" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="18"></td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-676a5fbc12d006e39e085d778d29dfef_l3.png?resize=53%2C19&ssl=1" alt="Phi^{-1}(y)" width="53" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="19"></td>
</tr>
<tr>
<td>recip</td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-00ebea95be7523fe01bd37fe98c097d1_l3.png?resize=27%2C15&ssl=1" alt="x^{-1}" width="27" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="15"></td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-b99d68a389204623e829b5e38f903490_l3.png?resize=26%2C19&ssl=1" alt="y^{-1}" width="26" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="19"></td>
</tr>
<tr>
<td>reverse</td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-6cacb15a7aa187378723791e7a017ae0_l3.png?resize=23%2C8&ssl=1" alt="-x" width="23" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="8"></td>
<td><img src="https://i0.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-ee7ffd10780498e6daa0b55f23b33c97_l3.png?resize=22%2C12&ssl=1" alt="-y" width="22" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="12"></td>
</tr>
<tr>
<td>sqrt</td>
<td><img src="https://i2.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-9549bd54d27ec02e8b4f3c5b05b012f4_l3.png?resize=31%2C17&ssl=1" alt="x^{1/2}" width="31" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="17"></td>
<td><img src="https://i1.wp.com/www.statworx.com/wp-content/ql-cache/quicklatex.com-c73c29a3e05add4949b9207310200e96_l3.png?resize=16%2C19&ssl=1" alt="y^2" width="16" title="Rendered by QuickLaTeX.com" data-recalc-dims="1" height="19"></td>
</tr>
</tbody>
</table>
</figure>
<pre><code># Transforming the axes 
plot_base + 
  coord_trans(x = &quot;log&quot;, y = &quot;log2&quot;) +
  ggtitle(&quot;coord_trans with x = \&quot;log\&quot; and y = \&quot;log2\&quot;&quot;)
</code></pre>
<p><img alt="transformed data" src="https://i1.wp.com/www.statworx.com/wp-content/uploads/plot-transform.jpg?w=450&ssl=1" class="wp-image-13900" data-recalc-dims="1"></p>
<h3>Swapping the axes</h3>
<p>The last of the Cartesian options, <em>cood_flip</em>, swaps x and y axis. For example, this option can be useful, when we intend to change the orientation of univariate plots as histograms or plot types – like box plots – that visualize the distribution of a continuous variable over the categories of another variable. Nonetheless, <em>coord_flip</em> also works with all other plots. This multiplies the overall possibilities for designing plots, especially since all Cartesian coordinate systems can be combined. </p>
<pre><code># Swapping axes 
# base plot #2
p1 &lt;- ggplot(data = df_iris) +
  geom_bar(aes(x = Species, fill = Species), alpha = 0.6) +
  scale_fill_manual(values = c(&quot;#693FE9&quot;, &quot;#A089F8&quot;, &quot;#4f5fb7&quot;)) +
  theme_minimal() 

# base plot &amp; coord_flip()
p2 &lt;- ggplot(data = df_iris) +
  geom_bar(aes(x = Species, fill = Species), alpha = 0.6) +
  scale_fill_manual(values = c(&quot;#693FE9&quot;, &quot;#A089F8&quot;, &quot;#4f5fb7&quot;)) +
  theme_minimal() +
  coord_flip() 

gridExtra::grid.arrange(p1, p2, top = &quot;Bar plot without and with coord_flip&quot;)
</code></pre>
<p><img alt="fliped coordinate system" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/plot-flip.jpg?w=450&ssl=1" class="wp-image-13897" data-recalc-dims="1"></p>
<h2>Polar coordinate system</h2>
<p>The customization of Cartesian coordinate systems allows for the fine tuning of plots. However, <em>coord_polar</em>, the final coordinate system discussed here, changes the whole character of a plot. By using <em>coord_polar</em>, bar geoms are transformed to pie charts or “bullseye” plots, while line geoms are transformed to radar charts. This is done by mapping x and y to the angle and radius of the resulting plot. By default, the x variable is mapped to the angle but by setting the <em>theta</em> augment in <em>coord_polar</em> to <em>“y”</em> this can be changed. </p>
<p><img alt="pie charts" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/coord-polar-bar.jpg?w=450&ssl=1" class="wp-image-13885" data-recalc-dims="1"><br>
<img alt="polygon plot" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/coord-polar-polygon.jpg?w=450&ssl=1" class="wp-image-13888" data-recalc-dims="1"></p>
<p>While such plots might shine with respect to novelty and looks, their perceptual properties are intricate, and their correct interpretation may be quite hard and rather unintuitive.</p>
<pre><code># Base plot 2 (long format, x = 1 is summed up to generate count)
plot_base_2 &lt;- df_iris %>%
  dplyr::mutate(x = 1) %>%
  ggplot(.) +
  geom_bar(aes(x = x, fill = Species), alpha = 0.6) + 
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank()) +
  scale_fill_manual(values = c(&quot;#693FE9&quot;, &quot;#A089F8&quot;, &quot;#4f5fb7&quot;)) +
  theme_minimal() +
  ggtitle(&quot;base plot&quot;)

# Bullseye plot 
# geom_bar &amp; coord_polar(theta = &quot;x&quot;)
p2 &lt;- plot_base_2 +
  coord_polar(theta = &quot;x&quot;) +
  ggtitle(&quot;theta = \&quot;x\&quot;&quot;)

# Pie chart 
# geom_bar &amp; coord_polar(theta = &quot;y&quot;)
p3 &lt;- plot_base_2 +
  coord_polar(theta = &quot;y&quot;) +
  ggtitle(&quot;theta = \&quot;y\&quot;&quot;)

gridExtra::grid.arrange(p2, p3, plot_base_2, top = &quot;geom_bar &amp; coord_polar&quot;, ncol = 2)
</code></pre>
<pre><code># Base plot 3 (long format, mean width/length of sepals/petals calculated)
plot_base_3 &lt;- iris %>%
  dplyr::group_by(Species) %>%
  dplyr::summarise(Petal.Length = mean(Petal.Length),
                   Sepal.Length = mean(Sepal.Length),
                   Sepal.Width = mean(Sepal.Width),
                   Petal.Width = mean(Petal.Width)) %>%
  reshape2::melt() %>%
  ggplot() +
  geom_polygon(aes(group = Species, color = Species, y = value, x = variable), 
  fill = NA) +
  scale_color_manual(values = c(&quot;#693FE9&quot;, &quot;#A089F8&quot;, &quot;#4f5fb7&quot;)) +
  theme_minimal() +
  ggtitle(&quot;base plot&quot;)

# Radar plot
# geom_polygon &amp; coord_polar
p2 &lt;- plot_base_3 +
  theme_minimal() +
  coord_polar() +
  ggtitle(&quot;coord_polar&quot;)

gridExtra::grid.arrange(plot_base_3, p2, top = &quot;geom_polygon &amp; coord_polar&quot;, ncol = 2)
</code></pre>
<p> </p>
<h2>References</h2>
<ul>
<li>Wickham, H. (2016). <em>ggplot2: elegant graphics for data analysis</em>. Springer.</li>
</ul>
<div>
<h6>Über den Autor</h6>
<p><img src="https://i0.wp.com/www.statworx.com/wp-content/uploads/statworx-avatar-female-300x300.png?resize=180%2C180&ssl=1" width="180" alt="Lea Waniek" class="wp-user-avatar wp-user-avatar-180" data-recalc-dims="1" height="180"></p>
<div>
<h4>Lea Waniek</h4>
<p>Lea ist Mitglied im Data Science Team und unterstützt ebenfalls im Bereich Statistik.</p>
</div>
</div>
<p>Der Beitrag <a rel="nofollow" href="https://www.statworx.com/de/blog/coordinate-systems-in-ggplot2-easily-overlooked-and-rather-underrated/" target="_blank">Coordinate systems in ggplot2: easily overlooked and rather underrated</a> erschien zuerst auf <a rel="nofollow" href="https://www.statworx.com/de" target="_blank">STATWORX</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.statworx.com/de/blog/coordinate-systems-in-ggplot2-easily-overlooked-and-rather-underrated/"> r-bloggers – STATWORX</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/caKm7e0TRrs" height="1"></a></div>