1

I am trying to create a gender-based pictogram chart using ggplot2 in R, where the size of the two pictograms should be proportional to the frequency of each gender. However, I am encountering some errors and would appreciate some help in resolving them. Here is what i tried so far:

*edit: I have changed the code on Mark's advice so that it is reproducible.

library(ggplot2)
library(ggimage)

# Creating  example Dataset----------------------------------------
sum <- matrix(c(36, 12), 1, 2, dimnames=list("Anzahl",c("male", "female")))
rel_sum <- sum / sum(sum)

# Selecting symbols and colors -----------------------------------------
par(family = "Segoe UI Symbol")
symbol_m <- "\u2642"
symbol_w <- "\u2640"

# Colors
f1 <- rgb(255, 255, 204, maxColorValue = 255)
f2 <- rgb(161, 218, 180, maxColorValue = 255)

colour_m <- f1
colour_w <- f2

p <- ggplot() +
  xlim(0, 2) +
  ylim(0, 1) +
  theme_void()

p <- p +
  geom_text(
    aes(x = 0.5, y = 0.5, label = symbol_m, color = colour_m),
    size = 10,
    hjust = rel_sum["male"]
  ) +
  geom_text(
    aes(x = 1.5, y = 0.5, label = symbol_w, color = colour_w),
    size = 10,
    hjust = rel_sum["female"]
  )

print(p)

I have already tried a few things and I am not sure whether these ggplot functions work as i use them here.

As i said, i want to create a dynamically sized gender-based pictogram chart using ggplot2. The problem with the above code is that although i get a graphic with coloured pictograms, on the one hand they do not have the right colours and above all unfortunately they are both the same size.

Does anyone have any ideas what else i could try?

Any assistance would be highly appreciated. Thank you!

jc_som
  • 11
  • 3
  • 1
    this might be handy:https://rud.is/rpubs/building-pictograms.html – Mark Jul 04 '23 at 10:53
  • Also welcome to StackOverflow! Please make your example be [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Mark Jul 04 '23 at 10:56
  • Thanks for the tip! I have now changed the code so that it should be reproducible. I am not sure if I have expressed myself clearly. I want to create a graph that shows a symbol for women and men, which are larger or smaller in relation to the respective frequencies. But I'll read through your link. Maybe I'll find something there. – jc_som Jul 04 '23 at 14:02
  • thank you for doing that! :-) I'll take a look at it now – Mark Jul 04 '23 at 14:39

0 Answers0