How to fill in area of geom_point in proportion to value? (e.g. Harvey balls)

Viewed 33

I would like to create geom_points whose area is filled in according to a proportion. Something like Harvey balls, although Harvey balls usually only map 0% 25% 50% 75% and 100% whereas in this case the fill could be anything from 0 to 100% e.g.

enter image description here

The geom_point doesn't need to be a circle, it could be a bar whose area fills up in proportion to the frac as well. So long as the area fill is proportional to fraction it'll work fine. e.g.

enter image description here

Reproducible data

library(tidyverse)  

df <- 
    tibble(
      x = c(1:5),
      y = c(5:1),
      yes = c(1, 14, 10, 42, 53),
      total = c(11, 21, 25, 50, 100),
      frac = yes/total)
    
df %>% 
  ggplot(
    aes(
      x = x,
      y = y)) + 
  geom_point(
    fill = "#000000",
    size = 7.5) 

enter image description here

2 Answers

Library scatterpie allows to display such plot.
You must have one variable by proportion you want to display, here: fracPos and fracNeg.

### Libraries
library(tidyverse)
library(dplyr)  
library(scatterpie)
library(ggthemes)

### Data-management
df <- 
  tibble(
    x = c(1:5),
    y = c(5:1),
    yes = c(1, 14, 10, 42, 53),
    total = c(11, 21, 25, 50, 100),
    fracPos = yes/total,
    fracNeg = 1- yes/total)

### Plot proportions
ggplot() +
  geom_scatterpie(data=df, aes(x=x, y=y), 
                  cols = c("fracPos", "fracNeg"), color="white", 
                  size = 1, pie_scale = 3) + 
  scale_fill_manual(values=c("orange", "black")) + 
  theme_hc() + 
  theme(legend.position="none")

enter image description here

Updated with Maël tips.

You can take a look at geom_scatterpie:

library(scatterpie)
df %>% 
  ggplot(aes(x = x, y = y)) +
  geom_scatterpie(data = df, cols = c("frac", "opp_frac"), 
                  size = 1, pie_scale = 3) + 
  coord_equal()

data

df <- 
  tibble(
    x = c(1:5),
    y = c(5:1),
    yes = c(1, 14, 10, 42, 53),
    total = c(11, 21, 25, 50, 100),
    frac = yes/total, 
    opp_frac = 1 - frac)

enter image description here

Related