geom_text 显示几个值

geom_text shows several values

我正在尝试创建一个按奖牌过滤的条形图。我希望我的图表以文本形式显示奖牌总数。我加入了两个数据集,这样我就得到了每个国家显示 3 次的奖牌总数。我希望我的图表只显示一次总和。

代码:

library(ggplot2)
library(dplyr)

c %>%
 ggplot(aes(x = factor(reorder(region, medals)), y = medals, fill = Medal)) +
 geom_col() +
 coord_flip() +
 geom_text(aes(label = med_count), vjust=1, colour="black",
           position=position_dodge(.9), size=3)

输出:

数据:

> dput(c)
structure(list(region = structure(c(
  34L, 34L, 34L, 153L, 153L, 153L, 68L, 68L, 68L, 198L, 198L, 198L, 
  63L, 63L, 63L, 136L, 136L, 136L, 178L, 178L, 178L, 12L, 12L, 12L, 
  179L, 179L, 179L, 88L, 88L, 88L, 49L, 49L, 49L, 64L, 64L, 64L, 130L, 
  130L, 130L, 171L, 171L, 171L, 40L, 40L, 40L), 
  .Label = c("Afghanistan", "Albania", "Algeria", "American Samoa", 
             "Andorra", "Angola", "Antigua", "Argentina", "Armenia",
             "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", 
             "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
             "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", 
             "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei",
             "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
             "Cameroon", "Canada", "Cape Verde", "Cayman Islands", 
             "Central African Republic", "Chad", "Chile", "China", 
             "Colombia", "Comoros", "Cook Islands", "Costa Rica", 
             "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic",
             "Democratic Republic of the Congo", "Denmark", "Djibouti", 
             "Dominica", "Dominican Republic", "Ecuador", "Egypt", 
             "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
             "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia",
             "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guam",
             "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti",
             "Honduras", "Hungary", "Iceland", "India", 
             "Individual Olympic Athletes", "Indonesia", "Iran", "Iraq",
             "Ireland", "Israel", "Italy", "Ivory Coast", "Jamaica", 
             "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", 
             "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", 
             "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein",
             "Lithuania", "Luxembourg", "Macedonia", "Madagascar",
             "Malawi", "Malaysia", "Maldives", "Mali", "Malta", 
             "Marshall Islands", "Mauritania", "Mauritius", "Mexico", 
             "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro",
             "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru",
             "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger",
             "Nigeria", "North Korea", "Norway", "Oman", "Pakistan", 
             "Palau", "Palestine", "Panama", "Papua New Guinea", 
             "Paraguay", "Peru", "Philippines", "Poland", "Portugal", 
             "Puerto Rico", "Qatar", "Refugee Olympic Team", 
             "Republic of Congo", "Romania", "Russia", "Rwanda", 
             "Saint Kitts", "Saint Lucia", "Saint Vincent", "Samoa",
             "San Marino", "Sao Tome and Principe", "Saudi Arabia", 
             "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
             "Slovakia", "Slovenia", "Solomon Islands", "Somalia",
             "South Africa", "South Korea", "South Sudan", "Spain",
             "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden",
             "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania",
             "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad", 
             "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda",
             "UK", "Ukraine", "United Arab Emirates", "Uruguay", "USA", 
             "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", 
             "Virgin Islands, British", "Virgin Islands, US", "Yemen", 
             "Zambia", "Zimbabwe"), class = "factor"), 
  med_count = c(447, 447, 447, 385, 385, 385, 373, 373, 373, 371, 
                371, 371, 269, 269, 269, 244, 244, 244, 227, 227, 
                227, 176, 176, 176, 144, 144, 144, 143, 143, 143, 
                106, 106, 106, 102, 102, 102, 87, 87, 87, 86, 86, 
                86, 80, 80, 80), 
  Medal = structure(c(1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 
                      1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 
                      1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 
                      1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L), 
                    .Label = c("Bronze", "Gold", "None", "Silver"), 
                    class = "factor"), 
  medals = c(58L, 230L, 159L, 103L, 169L, 113L, 81L, 147L, 145L, 109L, 
             80L, 182L, 167L, 24L, 78L, 68L, 89L, 87L, 81L, 85L, 61L, 
             70L, 51L, 55L, 66L, 40L, 38L, 65L, 39L, 39L, 66L, 27L, 13L,
             55L, 22L, 25L, 32L, 32L, 23L, 10L, 50L, 26L, 34L, 16L, 30L)), 
  row.names = c(NA, -45L), 
  class = c("tbl_df", "tbl", "data.frame"))

看看这是否是您要找的:

c %>%

  # process the variable values before passing the data frame to ggplot(),
  mutate(Medal = forcats::fct_relevel(Medal, "Gold", "Silver", "Bronze", "None"),
         region = forcats::fct_reorder(region, med_count)) %>%

  ggplot(aes(x = region, y = medals, fill = Medal)) +
  geom_col() +
  geom_text(aes(y = med_count,      # position the label based on total medals per region
                label = med_count),
            hjust = 0,              # left-align labels to total medal count 
                                    # (default is central alignment)
            nudge_y = 5,            # offset labels slightly to the right
            check_overlap = TRUE,   # for three labels overlapping in the same position, 
                                    # only plot the first one
            size=3) +

  # optional: a color palette that sort of rsembles the medal colours
  scale_fill_brewer(palette = "BrBG") +
  coord_flip()