I'm using the R
scatterpie
package for plotting pie charts on an xy plane. Each one of the pies/groups also has a class
assignment which I would also like to convey in the plot and was thinking of using patterns for that.
Here's the example data.frame
:
library(ggplot2)
library(ggpattern)
library(scatterpie)
library(dplyr)
set.seed(1)
df <- data.frame(x = runif(50, 0, 100), y = runif(50, 0, 100), id = paste0("id",1:50),
g1 = as.integer(runif(50, 0, 100)), g2 = as.integer(runif(50, 0, 100)), g3 = as.integer(runif(50, 0, 100)),
class = sample(c("c1", "c2"), 50, replace = T)) %>%
dplyr::mutate(size = 0.02*(g1 + g2 + g3))
df$class <- factor(df$class)
Here's the code I tried with the ggpattern
package:
ggplot() + geom_scatterpie(data = df, aes(x = x, y = y, group = id, r = size),cols = paste0("g",1:3),color = "black", alpha = 0.8, size = 0.1) +
scale_fill_manual(values=c("#FFC9F8", "#ee9e77", "#895A44")) +
geom_col_pattern(aes(pattern_fill = class))+
xlim(-10,110) + ylim(-10,110) + coord_equal() + theme_void()
Which gives the error:
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error in `f()`:
! Aesthetics must be valid data columns. Problematic aesthetic(s): pattern_fill = class.
Did you mistype the name of a data column or forget to add after_stat()?
I tried playing around with the pattern
part of the command but with no success.
Currently, the only way I am able to encode the class
of each pie is with the linetype
aesthetics argument:
ggplot() + geom_scatterpie(data = df, aes(x = x, y = y, group = id, r = size, linetype = class),cols = paste0("g",1:3), color = "black", size = 1) +
scale_fill_manual(values=c("#FFC9F8", "#ee9e77", "#895A44")) +
xlim(-10,110) + ylim(-10,110) + coord_equal() + theme_void()
But it's not ideal.
Any idea how to make this work or alternative ways to encode class
?