• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / ggplot2 / annotate ggplot2 / Getting started with ggforce – a ggplot2 extension package

Getting started with ggforce – a ggplot2 extension package

March 26, 2019 by cmdlinetips

ggforce: Accelerating ggplot2
ggforce: Accelerating ggplot2

ggforce, R package extension for ggplot, has got a big upgrade with lot of new functions. ggforce was introduced about to years ago with the aim to provide missing functionalities in ggplot2. ggforce provides a

a repository of geoms, stats, etc. that are as well documented and implemented as the official ones found in ggplot2.

The new version of ggforce has a number of new functionalities that will for sure “Accelerate ggplot2”. Finally, had a chance to play with ggforce. Here are some examples of three (of many) interesting features of ggforce.

You can install the released version of ggforce from CRAN with:

install.packages("ggforce")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("thomasp85/ggforce")

ggforce now has a website at https://ggforce.data-imaginist.com, with full documentation overview.

Annotating the plots you make is great way to make a plot easy to grasp. ggplot2 has functions like geom_text() and geom_label() to help annotating the data visualized using ggplot2. But, it is not exaggeration to say that it is not that easy to get it right. The new ggforce offers a set of geoms – geom_mark_*(), that makes it “easy to draw attention to, and describe, features of the plot. They all work in the same way, but differ in the way they enclose the area you want to draw attention to”

The 4 different geoms for annotating data on plots are:

  1. geom_mark_rect() encloses the data in the smallest enclosing rectangle]
  2. geom_mark_circle() encloses the data in the smallest enclosing circle
  3. geom_mark_ellipse() encloses the data in the smallest enclosing ellipse
  4. geom_mark_hull() encloses the data with a concave or convex hull

Let us see a quick example on how to use geom_mark_ellipse() on gapminder data set.

data_url = 'http://bit.ly/2cLzoxH'
gapminder = read_csv(data_url)
head(gapminder, n=3)

Let us first filter the gapminder data set to contain data for Africa and Europe for the year 2007.

gapminder_2007 <- gapminder %>% 
  filter(continent %in% c('Africa', 'Europe')) %>%
  filter(year == 2007) 

Let us first write the part of code to make a scatter plot between two variables, gdpPercap and lifeExp, with log scale on x-axis and store it in a variable “sc_plot”. So far this is exactly like plotting a scatterplot with ggplot2.

sc_plot <- gapminder_2007 %>%
 ggplot(aes(x=gdpPercap, lifeExp)) +
 geom_point() +
 scale_x_log10()

Now, we can add ggforce’s new function geom_mark_ellipse() with argument to fill color based on continent.

sc_plot + geom_mark_ellipse(aes(fill = continent))

Now we have annotated our ggplot with an ellipse. One can use other geom_mark_*() functions to annotate with different shapes.

How to annotate with ellipses in ggplot2 using ggforce?
How to annotate with ellipses in ggplot2 using ggforce?

In addition to annotating all the groups in our data, ggforce can also annotate selectively.
Basically, within geom_mark_*() functions we can select specific data points using filtering operation and annotate just those data, not all data in the plot.

# brief description to highlight data points
desc <- 'Europe has higher life expectancy' 
# select data points to annotate using filter within geom_mark_ellipse() 
gapminder_2007 %>%
  ggplot(aes(x=gdpPercap, lifeExp)) +
  geom_point() +
  scale_x_log10() + theme_bw(base_size = 16) +
  geom_mark_ellipse(aes(filter = continent == 'Europe', label = 'Europe', 
                        description = desc))

Here we have highlighted only European countries with an ellipse and a short text annotating what is special about those data points.

ggforce: annotate specific data in the plot
ggforce: annotate specific data in the plot

Isn’t that nice? Another new feature that is cool is ability to zoom into a plot made with ggplot2. Typically, to get the zoom-in view, one needs to manually limit the x-axis or y-axis values to show the data points of interest in a separate plot. Now with ggforce’s new function facet_zoom, one can create zoomed-in plots very easily. Here is an example of making a plot with zoom.

We will write the main plotting code like before and add the new layer facet_zoom(). With in facet_zoom(), we can specify which region of the plot that we want to zoom in. Here, we specify the condition for x-axis value to be equal to a specific country.

gapminder_2007 %>%
  ggplot(aes(x=gdpPercap, lifeExp)) +
  geom_point(aes(color=continent))+
  scale_x_log10()+
  facet_zoom(x = country == "Albania")

We we can see zoomed in view for specific x-axis value. Here we zoom-in on an European country that has low gdPercap compared to other European countries. We can also zoom in on y-axis or both x & y-axis.

ggforce zoom in with facet_zoom
ggforce zoom in with facet_zoom

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

How To Highlight Select Data Points with ggplot2 in R? Boxplot with jittered Data Points in RHow to Make Boxplot in R with ggplot2? ggplot2 change legend title with guides()How To Change Legend Title in ggplot2? Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R

Filed Under: annotate ggplot2, ggforce, ggforce facet_zoom, ggforce geom_mark_ellipse, ggplot2, R, R Tips, tidyverse 101 Tagged With: annotate ggplot2, ggforce, ggforce facet_zoom, ggforce geom_mark_ellipse

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version