Skip to main content

A circular bar chart with labels along the bars

Polar
Text
70 cities’ rainfall as bars bent around a circle with coord_radial(), each bar labelled along its own angle, flipped on the left half so nothing reads upside down.

A bar chart with 70 bars needs 70 labels, and on a page of ordinary width they don’t fit underneath. Bend the bars around a circle and each label gets the space at the end of its own bar. A straight bar chart is easier to compare precisely, but sorted by height, the circle still shows the shape of the data: a spiral from the wettest city down to the driest.

The data is precip, which ships with R: average annual precipitation for 70 cities in the United States and Puerto Rico, from the Statistical Abstract of the United States (1975). Two of the cities are called Portland. precip lists them in state order, so the first is Maine’s and the second Oregon’s.

city <- names(precip)
city[city == "Portland"] <- c("Portland, ME", "Portland, OR")
pr <- data.frame(city = city, cm = as.numeric(precip) * 2.54)
pr <- pr[order(-pr$cm), ]
pr$city <- factor(pr$city, levels = pr$city)

Labels along the angle

coord_radial() bends the x axis into the angle and the y axis into the radius. The labels need one more step, the same one a ggplot2 circular bar chart takes: work out each bar’s angle and turn its text to match. With 70 bars starting at twelve o’clock and running clockwise, bar i sits at 90° − 360° × (i − ½) / 70. On the left half of the circle that angle would put the text upside down, so those labels turn another 180° and are anchored at their right end (hjust = 1) instead of their left. The number goes on the inner side, so it always sits next to the bar.

n <- nrow(pr)
ang <- 90 - 360 * (seq_len(n) - 0.5) / n
pr$right <- ang > -90
pr$angle <- ifelse(pr$right, ang, ang + 180)
pr$label <- ifelse(pr$right,
                   paste0(pr$city, "  ", round(pr$cm)),
                   paste0(round(pr$cm), "  ", pr$city))
pr$label_at <- pr$cm + 4

What is drawn

  • mark_bar() with an explicit y draws one bar per city at its height, filled on a continuous scale from sand to deep teal.
  • Two mark_text() layers draw the labels, one for each half of the circle, each with its angle mapped and its own hjust.
  • The y limits start below zero at −45. That opens the hole in the middle: the radius is the y scale, so the bars start some way out from the centre, where the circle is wide enough to hold them.
  • theme_minimal() with the axis text turned off leaves only the circular gridlines, which show behind the shorter bars.
p <- vplot(pr, width = 7.5, height = 8) |>
  mark_bar(x = city, y = cm, fill = cm) |>
  mark_text(data = pr[pr$right, ], x = city, y = label_at, label = label,
            angle = angle, hjust = 0, size = 7) |>
  mark_text(data = pr[!pr$right, ], x = city, y = label_at, label = label,
            angle = angle, hjust = 1, size = 7) |>
  scale_fill_gradientn(colours = c("#e8d9b5", "#9fb3a1", "#3f6e78", "#1f3b4d")) |>
  scale_y_continuous(limits = c(-45, 185), breaks = c(50, 100, 150)) |>
  coord_radial() |>
  labs(
    title = "How wet is the year?",
    subtitle = "Average annual precipitation in 70 US cities, in centimetres, wettest first",
    caption = "Data: datasets::precip (Statistical Abstract of the United States, 1975)"
  ) |>
  theme_minimal() |>
  theme(
    legend.position = "none",
    plot.title = element_text(size = 18),
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor = element_blank()
  )

Circular bar chart of annual precipitation in 70 US cities, sorted from Mobile (170 cm) at twelve o'clock clockwise down to Phoenix (18 cm), each bar labelled with its city and value along its angle

Mobile, Alabama tops the list at 170 cm a year, followed by Miami, San Juan and New Orleans: the Gulf and the Caribbean. The bottom of the spiral is the desert Southwest and the interior West: Phoenix gets 18 cm, Reno and El Paso about 20. Most of the East sits in a narrow band around a metre, which is where the spiral flattens out.

For bars in polar space without the labels, see the coxcomb; for text that follows a curve rather than a straight angle, see labels that follow the curve.

Back to top