Optimize this code using functions.
# Create a pie chart
gender_count <- temp_df %>%
  group_by(GENDER) %>%
  summarize(count = n())

# Calculate percentage
gender_count$percentage <- gender_count$count / sum(gender_count$count) * 100


ggplot(gender_count, aes(x = "", y = count, fill = GENDER)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  geom_text(aes(label = paste0(round(percentage, 1), "%")), position = position_stack(vjust = 0.6)) +
  theme_void() +
  theme(legend.position = "right") +
  labs(title = "Proportion of Male and Female Recipients", x = NULL, y = NULL, fill = "Gender")


# Create a pie chart
gender_count_don <- temp_df %>%
  group_by(GENDER_DON) %>%
  summarize(count = n())

# Calculate percentage
gender_count_don$percentage <- gender_count_don$count / sum(gender_count_don$count) * 100


ggplot(gender_count_don, aes(x = "", y = count, fill = GENDER_DON)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  geom_text(aes(label = paste0(round(percentage, 1), "%")), position = position_stack(vjust = 0.6)) +
  theme_void() +
  theme(legend.position = "right") +
  labs(title = "Proportion of Male and Female Donors", x = NULL, y = NULL, fill = "Gender")