Apologies if this question is too simplistic, I feel like I see this type of chart all the time but I've never had to make one myself until now.
I'm teaching some non-mathematical people about how sample sizing works, and my example data look like this:
testid <- c(1, 1, 2, 2, 3, 3)
source <- rep(c("A", "B"), 3)
n <- c(1, 1, 100, 100, 100, 100)
p <- c(1, 0, 1, 2, 50, 51)
df <- data.frame(testid, source, n, p)
df
testid source n p
1 1 A 1 1
2 1 B 1 0
3 2 A 100 1
4 2 B 100 2
5 3 A 100 50
6 3 B 100 51
What I would like to do is represent this with images that have n
dots in a grid, of which p
dots are filled in. So my output would look like:
{1A: image of one filled-in dot}
{1B: image of one empty dot}
{2A: image of 100 empty dots in a 10x10 grid, of which one is filled in}
{2B: image of 100 empty dots in a 10x10 grid, of which two are filled in}
{3A: image of 100 empty dots in a 10x10 grid, of which 50 are filled in}
{3B: image of 100 empty dots in a 10x10 grid, of which 51 are filled in}
Is this do-able in R (any package)? If not, what tool is best for doing it?
CodePudding user response:
Sure could this be achieved in R
. As you want multiple charts I would write a function, which
- creates a grid of
n
points via e.g.expand.grid
- randomly chooses
p
grid points to fill - uses
ggplot2
to plot the grid
Depending on the number of charts you could use this function to make your charts one by one. A more elegant solution would be to make use of e.g. purrr::pmap
to loop over the rows of your dataframe to make your charts:
library(ggplot2)
library(purrr)
set.seed(123)
plot_grid <- function(n, p, ...) {
# Make grid
n <- round(sqrt(n))
d <- expand.grid(x = seq(n), y = seq(n))
d$fill <- 0
# Randomly choose p grid points to fill
d$fill[sample(seq(nrow(d)), p)] <- 1
# Plot
ggplot(d, aes(x, y, fill = factor(fill)))
geom_point(shape = 21, size = 10)
scale_fill_manual(values = c("0" = "white", "1" = "red"))
theme_void()
guides(fill = "none")
}
grid_plots <- purrr::pmap(df, plot_grid)
names(grid_plots) <- paste0(df$testid, df$source)
grid_plots$`3B`