Mepụta Histogram Animated Iji R

Mepụta Histogram Animated Iji R

Chaatị mmanya eserese nke enwere ike itinye ozugbo na post na weebụsaịtị ọ bụla na-aghọwanye ewu ewu. Ha na-egosipụta mgbanwe mgbanwe na njirimara ọ bụla n'ime oge ụfọdụ ma mee nke a nke ọma. Ka anyị hụ otu esi emepụta ha site na iji ngwugwu R na jeneriki.

Skillbox na-atụ aro: Usoro bara uru "Onye nrụpụta Python si ọkọ".

Anyị na -echetara: maka ndị na-agụ Habr niile - ego 10 ruble mgbe ị na-edebanye aha na nkuzi Skillbox ọ bụla site na iji koodu mgbasa ozi Habr.

ngwugwu

Anyị chọrọ ngwugwu na R:

Abụọ ndị a dị oke mkpa. Na mgbakwunye, a ga-achọrọ nhazi nke ọma, onye na-elekọta ụlọ na akpịrịkpa iji jikwaa data ahụ, hichaa nhazi na nhazi ya.

data

A na-ebudata ihe ndekọ mbụ anyị ga-eji rụọ ọrụ a na webụsaịtị Bank World. Lee ha bụ - WorldBank data. Otu data ahụ, ọ bụrụ na ịchọrọ ya emebere ya, enwere ike ibudata ya nchekwa nchekwa.

Kedu ụdị ozi nke a bụ? Ihe nlele ahụ nwere uru GDP nke ọtụtụ mba ruo ọtụtụ afọ (site na 2000 ruo 2017).

Nhazi data

Anyị ga-eji koodu ezigara n'okpuru iji kwado usoro data achọrọ. Anyị na-ehichapụ aha kọlụm, gbanwee ọnụọgụgụ ka ọ bụrụ usoro ọnụọgụ, wee gbanwee data site na iji ọrụ mkpokọta (). Anyị na-echekwa ihe niile enwetara na gdp_tidy.csv maka iji ọzọ.

library(tidyverse)
library(janitor)

gdp <- read_csv("./data/GDP_Data.csv")

#select required columns

gdp <- gdp %>% select(3:15)

#filter only country rows

gdp <- gdp[1:217,]

gdp_tidy <- gdp %>%
mutate_at(vars(contains("YR")),as.numeric) %>%
gather(year,value,3:13) %>%
janitor::clean_names() %>%
mutate(year = as.numeric(stringr::str_sub(year,1,4)))

write_csv(gdp_tidy,"./data/gdp_tidy.csv")

Histogram nke eserese

Ihe okike ha chọrọ usoro abụọ:

  • Iji ggplot2 na-eme atụmatụ ihe ndekọ akụkọ ihe mere eme n'ezie.
  • Mee ihe nkiri static static site na iji paramita achọrọ site na iji ganimate.

Nzọụkwụ ikpeazụ bụ ịmegharị animation n'ụdị achọrọ, gụnyere GIF ma ọ bụ MP4.

Na-ebunye ọba akwụkwọ

  • ụlọ akwụkwọ (ụlọ akwụkwọ)
  • ụlọ akwụkwọ (ganimate)

Njikwa data

N'ime usoro a, ịkwesịrị inyocha data ahụ iji nweta mba iri kacha elu maka afọ ọ bụla. Ka anyị tinye ọtụtụ kọlụm ga-enye anyị ohere igosipụta akụkọ akụkọ maka histogram.

gdp_tidy <- read_csv("./data/gdp_tidy.csv")

gdp_formatted <- gdp_tidy %>%
group_by(year) %>%
# The * 1 makes it possible to have non-integer ranks while sliding
mutate(rank = rank(-value),
Value_rel = value/value[rank==1],
Value_lbl = paste0(" ",round(value/1e9))) %>%
group_by(country_name) %>%
filter(rank <=10) %>%
ungroup()

Nrụpụta histogram static

Ugbu a anyị nwere ngwugwu data n'ụdị achọrọ, anyị na-amalite ịbịaru static histogram. Ozi bụ isi - mba 10 kachasị nwere oke GDP maka oge ahọpụtara. Anyị na-emepụta eserese maka afọ ọ bụla.

staticplot = ggplot(gdp_formatted, aes(rank, group = country_name,
fill = as.factor(country_name), color = as.factor(country_name))) +
geom_tile(aes(y = value/2,
height = value,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(country_name, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(y=value,label = Value_lbl, hjust=0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x = element_line( size=.1, color="grey" ),
panel.grid.minor.x = element_line( size=.1, color="grey" ),
plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
plot.caption =element_text(size=8, hjust=0.5, face="italic", color="grey"),
plot.background=element_blank(),
plot.margin = margin(2,2, 2, 4, "cm"))

Ịmepụta nkata site na iji ggplot2 dị nnọọ mfe. Dịka ị na-ahụ na ngalaba koodu dị n'elu, enwere ọtụtụ isi ihe nwere isiokwu() ọrụ. Ha dị mkpa ka ihe niile na-eme ndụ na-enweghị nsogbu. Ụfọdụ n'ime ha agaghị egosipụta ma ọ dị mkpa. Ọmụmaatụ: Naanị ahịrị grid kwụ ọtọ na akụkọ ifo ka a na-adọta, mana aha axis na ọtụtụ ihe ndị ọzọ na-ewepụ na mpaghara ahụ.

-aga aga

Isi ọrụ ebe a bụ Transtition_states(), ọ na-ejikọta ọnụ eserese static dị iche iche. A na-eji view_follow() see ahịrị grid.

anim = staticplot + transition_states(year, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'GDP per Year : {closest_state}',
subtitle = "Top 10 Countries",
caption = "GDP in Billions USD | Data Source: World Bank Data")

Na-ere

Ozugbo emepụtara animation ma chekwaa ya na ihe anim, ọ bụ oge iji mee ya site na iji ọrụ animate (). Onye nrụpụta eji na animate() nwere ike ịdị iche dabere n'ụdị faịlụ mmepụta achọrọ.

GIF

# For GIF

animate(anim, 200, fps = 20, width = 1200, height = 1000,
renderer = gifski_renderer("gganim.gif"))

MP4

# For MP4

animate(anim, 200, fps = 20, width = 1200, height = 1000,
renderer = ffmpeg_renderer()) -> for_mp4

anim_save("animation.mp4", animation = for_mp4 )

N'ihi

Mepụta Histogram Animated Iji R

Dị ka ị pụrụ ịhụ, ọ dịghị ihe mgbagwoju anya. Ọrụ niile dị na ya GitHub m, ị nwere ike iji ya dịka ịchọrọ.

Skillbox na-atụ aro:

isi: www.habr.com

Tinye a comment