aa5
aa5

Reputation: 11

Attaching half circles to a bar plot

I am trying to add half circles to a bar plot, with width of each bar of a bar plot being different (something like the attached hand drawn figure). Is it possible to do in R?

I searched for examples, but I did not find any. May be I am using wrong keywords. So I will appreciate any help pointing me to an example.

Hand drawn figure

Upvotes: 0

Views: 64

Answers (1)

teunbrand
teunbrand

Reputation: 38023

So here is a very basic example using ggforce:

library(ggplot2)
library(ggforce)

df <- data.frame(x = c(1:3), y = c(2.3, 1.9, 3.2),
                 cat = LETTERS[1:3])
ggplot(df, aes(x, y)) +
  geom_col(aes(fill = cat)) +
  # default width of a bar is 0.9, so inner (r0) and outer (r) radius
  # should be nudged by 0.45
  geom_arc_bar(aes(x0 = 0, y0 = 0, # center
                   r = x + 0.45, r0 = x - 0.45, # radii
                   start = 0.5 * pi, end = 1.5*pi, # position in radians
                   fill = cat),
               inherit.aes = FALSE, colour = NA)

enter image description here

Now if the width is reflective of some variable, the radii should reflect this. Take care that the bars don't start overlapping with oneanother.

df <- data.frame(x = c(1:3), y = c(2.3, 1.9, 3.2),
                 cat = LETTERS[1:3],
                 widthvar = c(0.5, 1, 0.75))
ggplot(df, aes(x, y)) +
  geom_col(aes(fill = cat, width = widthvar)) +
  geom_arc_bar(aes(x0 = 0, y0 = 0, r = x + 0.5 * widthvar, r0= x - 0.5 * widthvar, 
                   start = 0.5 * pi, end = 1.5*pi, fill = cat),
               inherit.aes = FALSE, colour = NA) +
  scale_x_continuous()

enter image description here

If your case is more complex than this, I'd recommend you provide a reproducible example that builds the bargraph portion of your question.

Upvotes: 1

Related Questions