df_countries <- df.export %>% pivot_longer(!Country,names_to ="year", values_to ="value") %>%
mutate(year = as.numeric(year)) %>%
select(c(Country,year,value))
# Impute missing values in value with their mean
df_countries<- df_countries %>%
mutate(value = ifelse(is.na(value), mean_value, value))
#Using ggplot to show the total values in different countries as the sample
# The distribution on export country in 2021
tb_inc <- df_countries %>%
filter(year == 2021) %>%
group_by(Country) %>%
summarise(value = sum(value, na.rm = TRUE)) %>%
ungroup()
tb_inc <- tb_inc %>%
mutate(country=recode(Country,
"United States"="USA",
"United Kingdom"="UK",
"Republic of Korea"="Korea"))
tb_map <- left_join(world_map, tb_inc, by=c("region"="country"))
ggplot(tb_map) +
geom_polygon(aes(x=long, y=lat, group=group, fill= value)) +
theme_map() +
scale_fill_viridis(na.value = "grey", name="ExportValue")ggplot(df.export2) +
geom_line(aes(x=Year, y=NaturalGas,colour="NaturalGas"),linewidth=1.5)+
geom_line(aes(x=Year, y=IronOre,colour = "IronOre"),linewidth=1.5)+
geom_line(aes(x=Year, y=Gold,colour = "Gold"),linewidth=1.5)+
geom_line(aes(x=Year, y=Coal,colour = "Coal"),linewidth=1.5)+
geom_point(shape=1, aes(x=Year, y=NaturalGas),size=2)+
geom_point(shape=2, aes(x=Year, y=IronOre),size=2)+
geom_point(shape=5, aes(x=Year, y=Coal),size=2)+
geom_point(shape=11, aes(x=Year, y=Gold),size=2)+
theme(legend.title = element_blank(),
legend.position = 'bottom',
axis.title.x = element_text(size=11, face = 'bold'),
axis.title.y = element_text(size=11, face='bold'),
legend.box.background = element_rect(colour = 'black'),
legend.background = element_blank())+
scale_x_continuous(breaks = round(seq(min(df.export2$Year), max(df.export2$Year), by = 1),1)) +
labs(y='Commodity', x='Year')ggplot(df.export2) +
geom_bar(aes(x = Year, y = IronOre, fill = "IronOre"), stat = "identity", color = "green", size = 0.5) +
geom_line(aes(x = Year, y = Education, color = "Education"), stat = "identity", group = 1, size = 1.5) +
geom_text(aes(label = round(IronOre), x = Year, y = IronOre), color = "blue") +
geom_text(aes(label = round(Education), x = Year, y = 0.9 * Education), color = "blue") +
ggtitle("Comparison on Export of Iron Ore and Education from 2016 to 2021") +
ggeasy::easy_center_title() +
theme(legend.title = element_blank(),
legend.position = "bottom",
axis.title.x = element_text(size = 11, face = "bold"),
axis.title.y = element_text(size = 11, face = "bold"),
legend.box.background = element_rect(colour = "black"),
legend.background = element_blank()) +
scale_fill_manual(values = "#87CEEB") +
scale_color_manual(values = c(Education = "purple")) +
scale_x_continuous(breaks = round(seq(min(df.export2$Year), max(df.export2$Year), by = 1), 1)) +
labs(y = "Commodity", x = "Year")