Code
Rank | NOC | Gold | Silver | Bronze | Total |
---|---|---|---|---|---|
1 | United States‡ | 40 | 44 | 42 | 126 |
2 | China | 40 | 27 | 24 | 91 |
3 | Japan | 20 | 12 | 13 | 45 |
4 | Australia | 18 | 19 | 16 | 53 |
5 | France* | 16 | 26 | 22 | 64 |
6 | Netherlands | 15 | 7 | 12 | 34 |
ggimage
geom_flags
from package ggimage enables using flags in ggplot2
Fazliddin Sultonov
Nov 16, 2024
Data Source: Wikipedia
Rank | NOC | Gold | Silver | Bronze | Total |
---|---|---|---|---|---|
1 | United States‡ | 40 | 44 | 42 | 126 |
2 | China | 40 | 27 | 24 | 91 |
3 | Japan | 20 | 12 | 13 | 45 |
4 | Australia | 18 | 19 | 16 | 53 |
5 | France* | 16 | 26 | 22 | 64 |
6 | Netherlands | 15 | 7 | 12 | 34 |
(Ustun nomlarini o’zgartirib davlat nomlaridagi kerakmas string larni olib tashlash)
Numer | Davlat | Oltin | Kumush | Bronza | Jami |
---|---|---|---|---|---|
1 | United States | 40 | 44 | 42 | 126 |
2 | China | 40 | 27 | 24 | 91 |
3 | Japan | 20 | 12 | 13 | 45 |
4 | Australia | 18 | 19 | 16 | 53 |
5 | France | 16 | 26 | 22 | 64 |
6 | Netherlands | 15 | 7 | 12 | 34 |
(jadvalga countycode
paketidagi isocodi ustunini qo’shish)
# usul #1
# df$cc <- countrycode::countrycode(
# sourcevar = df$Davlat,
# origin = "country.name",
# destination = "iso2c")
# usul #2 dplyr
data <- df |>
slice_head(n = 20) |>
mutate(
cc = countrycode::countrycode(
sourcevar = Davlat,
origin = "country.name.en",
destination = "iso2c")
) |>
select(-Numer)
head(data) |>
kable()
Davlat | Oltin | Kumush | Bronza | Jami | cc |
---|---|---|---|---|---|
United States | 40 | 44 | 42 | 126 | US |
China | 40 | 27 | 24 | 91 | CN |
Japan | 20 | 12 | 13 | 45 | JP |
Australia | 18 | 19 | 16 | 53 | AU |
France | 16 | 26 | 22 | 64 | FR |
Netherlands | 15 | 7 | 12 | 34 | NL |
Jadvalni grafik yasash holatiga ob kelish
# povot data
pv_data <- data |>
mutate(
cc = base::tolower(cc)
) |>
pivot_longer(
cols = c("Oltin", "Kumush", "Bronza"),
names_to = "Medal",
values_to = "Count"
)
# Grafikda umumiy medallar sonini kursatisch uchun
df_plt <- pv_data |>
group_by(Davlat) |>
summarise(Total = sum(Count)) |>
ungroup()
df_plt |>
kable()
Davlat | Total |
---|---|
Australia | 53 |
Brazil | 20 |
Canada | 27 |
China | 91 |
France | 64 |
Germany | 33 |
Great Britain | 65 |
Hungary | 19 |
Ireland | 7 |
Italy | 40 |
Japan | 45 |
Kenya | 11 |
Netherlands | 34 |
New Zealand | 20 |
Norway | 8 |
South Korea | 32 |
Spain | 18 |
Sweden | 11 |
United States | 126 |
Uzbekistan | 13 |
(grafik yasash)
my_color <- c("Oltin" = "gold", "Bronza" = "#cd7f32", "Kumush" = "#C0C0C0")
plt <- ggplot(
data = pv_data, aes(
x = reorder(Davlat, Count),
y = Count,
fill = Medal,
label = Count)) +
geom_bar(stat = "identity") +
#scale_y_continuous(limits = c(0,130),breaks = seq(0, 130, 10)) +
geom_text(size = 2.7, colour = "blue", # balken ichidagi text
position = position_stack(vjust = 0.5))+
geom_text(data = df_plt, # tashqaridagi text
aes(x = reorder(Davlat, Total),
y = Total, label = paste("= ", Total),
fill = NULL),
size = 3.0, family = "serif", fontface = "bold",
nudge_y = 5)+
scale_fill_manual(values = my_color) +
coord_flip() + # rotatsya axis
expand_limits(y = -3) +
theme_minimal()
plt
(Grafikka jilov berish, sarlavha, text. rang, etc.)
plt +
labs(title = "Parij 2024:",
subtitle = "Medallar bo'yicha top yigirmata davlatlar",
caption = "Data Source: Wikipedia, Plotting by ФС: @sultanof.uzb",
x = "",
y = "medallar soni") +
theme(plot.title = element_text(color = "dodgerblue4", size = 12,
face = "bold", hjust = 0.5),
plot.subtitle = element_text(color = "dodgerblue4", size = 10,
face = "bold", hjust = 0.5),
plot.caption = element_text(color = "dodgerblue", size = 10,
face = "italic"),
plot.background = element_rect(fill = "aliceblue"),
legend.title = element_blank(),
legend.background = element_blank(),
legend.position = "top") +
ggimage::geom_flag(aes(y = -3, image = cc), size = 0.07) -> p_parij
p_parij