Creating Half Moon Charts with ggplot2: A Step-by-Step Guide

Introduction to Half Moon Charts with ggplot2

Half moon charts are a type of polar chart that display data in a half-circle format. They are often used to visualize categorical data or to show the proportion of different categories within a dataset. In this article, we will explore how to create a half moon chart using ggplot2, a popular R package for creating high-quality statistical graphics.

Prerequisites

Before diving into the tutorial, it’s assumed that you have some experience with R and ggplot2. If not, don’t worry! This article will provide a step-by-step guide on how to create a half moon chart using ggplot2.

Installing Required Packages

To start creating half moon charts with ggplot2, you need to install the following packages:

library(tidyr)
library(dplyr)
library(ggplot2)
library(scales)

Example Data

We’ll use the following example data to illustrate how to create a half moon chart with ggplot2. This data includes three categories: Grain (%), Straw (%), and an ID column.

# Create sample data
set.seed(13)

dat <- tibble(
  id = 1:20,
  `Grain (%)` = runif(20, .525, .625),
  `Straw (%)` = 1 - `Grain (%)`
)

Data Preparation

Before creating the chart, we need to prepare our data. This includes:

  • Reordering the id column based on the Grain (%) values in descending order.
  • Merging the Grain (%) and Straw (%) columns into a single column called pct.
  • Creating separate data frames for the grid lines, labels, and other elements.
# Reorder id column by Grain (%)
dat %>% 
  mutate(id = reorder(id, `Grain (%)`, decreasing = TRUE))

# Merge Grain (%) and Straw (%) columns into a single column called pct
dat %>% 
  pivot_longer(!id, names_to = "type", values_to = "pct")

# Create separate data frames for grid lines, labels, etc.
panel <- expand_grid(
  x.lab = c(-.25, 21.25), 
  x.grid = c(0, 21),
  y = seq(0, .7, by = .1)
)

Creating the Half Moon Chart

Now that we have our data prepared, let’s create the half moon chart using ggplot2!

# Create the main plot
ggplot(dat, aes(id, pct)) +
  geom_line(
    data = panel,
    aes(x.grid, y, group = y),
    linewidth = .1,
    color = "gray70"
  ) +
  geom_text(
    data = panel,
    aes(x.lab, y, label = percent(y)),
    size = 8 / .pt
  ) +
  geom_line(aes(group = id), color = "gray50") +
  geom_point(aes(color = type), size = 3) +
  scale_x_discrete(expand = expansion(add = c(12, 12))) +
  scale_y_continuous(limits = c(0, .7), expand = c(0, 0)) +
  coord_polar(start = pi) +
  guides(color = guide_legend(direction = "horizontal")) +
  theme_void() +
  theme(
    axis.text.x = element_text(size = 8),
    panel.grid.major.x = element_line(linewidth = .1, color = "gray70"),
    legend.position = c(.5, .45),
    legend.title = element_blank()
  )

Customizing the Chart

As you can see, this chart is quite basic. Let’s customize it to make it more visually appealing!

Adjusting Colors and Fonts

You can adjust the colors and fonts used in your chart by modifying the color argument in the geom_line() function.

# Change line color
ggplot(dat, aes(id, pct)) +
  geom_line(
    data = panel,
    aes(x.grid, y, group = y),
    linewidth = .1,
    color = "blue"
  ) +

# Change text font size and style
theme(axis.text.x = element_text(size = 10, family = "sans-serif"),
      axis.text.y = element_text(size = 8, family = "sans-serif"))

Customizing the Axis

You can customize the axis by modifying its limits, labels, and styles using the scale_x_continuous() function.

# Set x-axis limits
ggplot(dat, aes(id, pct)) +
  scale_x_discrete(expand = expansion(add = c(12, 12))) +
  scale_y_continuous(limits = c(0, .7), expand = c(0, 0)) +

# Add axis labels and titles
theme(axis.title.x = element_text(size = 10, family = "sans-serif"),
      axis.text.x = element_text(size = 8, family = "sans-serif"))

Conclusion

In this article, we explored how to create a half moon chart using ggplot2. We covered the necessary steps for preparing the data, creating the main plot, and customizing the chart’s appearance.

We also discussed some best practices and tips for creating high-quality statistical graphics with ggplot2. Whether you’re a beginner or an advanced user, this tutorial should provide you with valuable insights into how to create stunning half moon charts!

With these skills, you’ll be well-equipped to tackle more complex data visualization tasks and share your findings effectively with others!


Last modified on 2023-09-29