如何将这两个图形组合成一个堆叠条形图? (以百分比显示计数)

How can I combine these two graphs into one stacked bar chart? (Displaying the counts as percentages)

我想将 cleaned_wbdata 的图表和 totalprices 的图表组合成一个单一的堆叠条形图,理想情况下将计数显示为百分比。任何帮助,将不胜感激。 数据集示例:

#libraries
library(jsonlite)
library(tidyverse)
library(data.table)
library(fst)
library(readxl)
library(readr)
library(countrycode)
library(foreign)
library(fastDummies)
#example data sets
cleaned_wbdata <- data.frame(c("Germany", "Germany", "Germany", "Austria", "Poland", "Poland")) %>%
  rename("send_country" = 1) %>%
  mutate(continent = send_country) %>%
  mutate(continent = countrycode(continent, origin = "country.name", destination = "continent"))
# other data set
totalprices <- data.frame(c("Poland", "Germany", "Germany", "Austria", "Poland", "Poland")) %>%
  rename("send_country" = 1) %>%
  mutate(continent = send_country) %>%
  mutate(continent = countrycode(continent, origin = "country.name", destination = "continent"))
  rename("send_country" = 1)
# first bar graph
cleaned_wbdata %>%
  filter(continent == "Europe") %>%
  group_by(send_country) %>%
  summarise(count = n()) %>%
  ungroup() %>%
  top_n(n = 10, wt = count) %>%  
  ggplot() +
  aes(x = reorder(send_country,count), y = count) +
  geom_bar(stat = "identity", fill = "darksalmon") +
  coord_flip() +
  theme_light() +
  labs(title = "Number of Remittances Sent by European Countries in 2020, World Bank") 
# second bar graph
totalprices %>%
  group_by(send_country) %>%
  filter(continent == "Europe") %>%
  summarise(count = n()) %>%
  ungroup() %>%
  top_n(n = 10, wt = count) %>%  
  ggplot() +
  aes(x = reorder(send_country,count), y = count) +
  geom_bar(stat = "identity", fill = "darkkhaki") +
  coord_flip() +
  theme_light() +
  labs(title = "Number of Remittances Sent by European Countries in 2020, Other Data") 

您是否正在寻找这样的东西:

  list(cleaned_wb_data = cleaned_wbdata, totalprices = totalprices) %>%
  purrr::imap(~filter(.x, continent == "Europe") %>%
         count(send_country) %>%
         top_n(n = 10, wt = n) %>%
         mutate(data_set = .y)
  ) %>%
  bind_rows() %>%
  group_by(send_country) %>%
  mutate(p = n / sum(n)) %>%
  ungroup() %>%
  ggplot(aes(send_country, n, fill = data_set)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = scales::percent_format()(p)), position = position_stack(vjust = 0.5)) +
  labs(y = NULL) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )