Sunday, May 30, 2021

Bar Chart Animation with R

In this blog, I illustrate about how we can build bar chart race in R that is used to visualize the evolution of a variable over time. I will show the evolution of public debt in top 25 countries in the world over the period of 2007 to 2020. The dataset has been taken from IMF WEO 2021 (April) database available here.   

The dataset as well as the R script files are available here

The first step is to import the dataset in R.

rm(list=ls())
setwd("C:/Users/SIDDHARAJ BHATTA/Desktop/R for Data Analysis")
debt <- read.csv("debt.csv")

In the second step, we convert the data from wide to long format by using the tidyr package.

library(tidyr)
names(debt)<-as.character(2003:2020)
debt_long <- gather(data=debt, key=Year, value=debt, 4:18, factor_key=TRUE)
debt_long$Year <- as.numeric(as.character(debt_long$Year))
debt_long$country<-debt_long$`2005`

In the third step, we rank the countries according to debt GDP ratio and filter the top 25 countries using dplyr package.

library(dplyr)
names(debt_long)
data<-debt_long %>%
  group_by(Year) %>%
  arrange(Year, -debt) %>%
  mutate(rank = 1:n()) %>%
  filter(rank <= 25)

 We will use ggplot2 and ggnimate package to plot the bar chart and animate it.

library(ggplot2)
library(gganimate)

data %>%  
  ggplot() +  
  aes(xmin = 0 ,  
      xmax = debt) +  
  aes(ymin=rank-0.45,
      ymax=rank+0.45,
      y = rank) +  
  facet_wrap(~ Year) +  
  geom_rect(alpha = .7, fill="darkblue") +  
  scale_x_continuous(  
    limits = c(0, 500),  
    breaks = c(0, 100, 300, 500)) +  
  geom_text(col = "darkblue",  
            hjust = "right",  
            aes(label = country),  
            x = -50, size=6) +  
  scale_y_reverse() +  
  labs(fill = NULL) +  
  labs(x = 'Debt GDP Ratio') +  
  labs(y = "") +
  theme_classic()->  
  my_plot

plot<-my_plot +
  theme_classic()+
  facet_null() +  
  scale_x_continuous(  
    limits = c(-500, 600),  
    breaks = c(0, 200, 400, 600)) +  
  geom_text(x = 500 , y = -2,  
            family = "Times",  
            aes(label = as.character(Year)),  
            size = 14, col = "darkblue") +
  geom_text(col = "white",  
            hjust = "right",  
            aes(label = debt),  
            x = 80, size=5) +
  ggtitle("Outstanding Public Debt  as percent of GDP")+
  xlab("Debt GDP Ratio")+
  ylab("Top 25 Countries")+
  theme(
    panel.background = element_rect(fill='lightblue', size=0.5,
                                    linetype=0, colour='lightblue'),
    plot.title = element_text(color="red", size=16, hjust=0.5, face="bold.italic"),
    axis.title.x = element_text(color="darkblue", size=13, face="bold"),
    axis.title.y = element_text(color="darkblue", size=13, face="bold")
  ) +
    gganimate::transition_time(Year)

animate(plot, nframes=14, fps=1, width=800)
 

The following lines of codes produce the Output in GIF Format. 

p1<-animate(plot, nframes=14,  fps = 1,  width = 800,
            renderer = gifski_renderer())

anim_save("animation.gif", animation = p1 )

The video explanation of this process is available here. 

I can be reached at siddhabhatta@gmail.com.


Monday, May 24, 2021

Impact of COVID-19 on Nepalese Economy : Summary of the Findings from the NRB Follow-Up Survey

Nepal Rastra Bank, the Central Bank of Nepal has published the Second Follow-up Report on ' Impact of COVID-19 on Nepalese economy on May 19, 2021.  The first phase of the survey was conducted in July 2020, the first follow-up survey in November 2020 and the second follow-up survey was conducted in April 2021. 

The first phase of the survey covered 674 businesses operating across 52 districts of the country while 604 businesses out of the 674 participated in the first follow-up and 562 businesses participated in the second follow-up survey.

The results from the survey show that the business activities as well as employment situation has improved significantly compared to July 2020. While only 4.1 percent of the businesses were operating at full scale, such share of firms increased to 54 percent in November 2020 and 81.2 percent in April 2021. Consequently, the share of firms in non-operation status steadily decreased from 61 percent in July 2020 to 4.2 percent in April 2021. 


All types of business have experienced improvements but the rate of improvements differ. The businesses from mining, transport and storage, and tourism sector seems to have lower rate of revival. As of April 2021, about 20 percent of the business from mining sector, 16.7 percent from transport and storage sector and 8.3 percent of the businesses from the hotel sector were not in non-operation status. On the other hand, all of the businesses of electricity, gas and water, and communication sector had operated at full level.

According to the size of businesses. while non of the large industries were in non-operation status in April 2021, around 6 percent of the cottage as well as micro businesses were in non-operation status. 

Taking the pre-COVID production level to be 100, all types of businesses were found to be below the pre-COVID level. The businesses were found to be operating at 28.8 percent of the pre-COVID output in July 2020 and 50.5 percent in November 2020. Such a situation improved to 61.4 percent in April 2021. 

Health services related businesses, agro-businesses, construction related businesses and manufacturing related businesses were found to be producing more than 70 of the pre-COVID output while hotel and restaurant businesses were found to be operating at 41 percent of the Pre-COVID level of output only.   

In terms of the size of the businesses, medium as well as large sized businesses seem to have recovered faster than other businesses. In April 2021, the medium as well as large businesses were found to be producing 65 and 73 percent of their respective pre-COVID level of output.   

Employment situation in April 2021 seem to have recovered compared  to July 2020.  Compared to the  pre-COVID level (=100), the average employment in the businesses reached 93.8 percent. Such a figure was 87.5 percent in November 2020 and 77.5 percent in July 2020. 
Employment seems to have recovered faster in micro, cottage  and small businesses compared to middle and large industries.  

The wage and salary expenses of the businesses has increased during the survey periods. In July 2020, businesses spent 63.4 percent of the amount they used to at the pre-COVID level.  Such amount increased to 70.4 percent in November 2020 and 88.5 percent in April 2021. 

The data file in excel format can be downloaded from here.  The blogger can be contacted at siddhabhatta@gmail.com .

Permanent link of the report is here


Wednesday, May 12, 2021

Current Macroeconomic Situation of Nepal

Current Macroeconomic Situation of Nepal (Based on Nine months Data -Mid-July 2020 to Mid-April 2021)

  • The y-o-y consumer price inflation stood at 3.10 percent.

  • Merchandise exports increased 20.2 percent to Rs.94.77 billion compared to an increase of 12.9 percent in the same period of the previous year.

  • Merchandise imports increased 13.1 percent to Rs.1111.40 billion against a decrease of 7.5 percent a year ago.

  • Total trade deficit increased 12.5 percent to Rs.1016.63 billion in nine months of 2020/21. Such deficit had decreased 8.9 percent in the same period of the previous year.

     

  • Remittance inflows increased 16.5 percent to Rs.729.02 billion in the review period against a decrease of 4.2 percent in the same period of the previous year.

  • Number of Nepali workers taking approval for foreign employment decreased 66.7 percent in the review period. It had increased 10.2 percent in the same period of the previous year.

  • The current account remained at a deficit of Rs.207.41 billion in the review period compared to a deficit of Rs.126.09 billion in the same period of the previous year.

  • Balance of Payments (BOP) registered a surplus of Rs.42.54 billion in the review period as compared to a surplus of Rs.36.61 billion in the same period of the previous year.

  • Gross foreign exchange reserves increased 2.2 percent to Rs.1433.27 billion in mid-April 2021 from Rs.1401.84 billion in mid-July 2020.

  • The foreign exchange reserves of the banking sector is sufficient to cover the prospective merchandise imports of 11.9 months, and merchandise and services imports of 10.8 months.

  • Nepalese currency vis-à-vis the US Dollar appreciated 0.5 percent in mid-April 2021 from mid-July 2020. It had depreciated 10.2 percent in the same period of the previous year.

  • Total expenditure of the federal government according to data of Financial Comptroller General Office(FCGO), Ministry of Finance, stood at Rs.690.28billion.

  • Revenue mobilization(including the amount to be transferred to provincial and local governments) stood at Rs.683.87billion.

  • Domestic debt mobilization of federal government amounted Rs.112.52billion, including Rs.20.5billion through treasury bills, Rs.90 billion through development bonds, Rs.2 billion through citizen saving certificates and Rs.15.3million through foreign employment bond.

  • Broad money (M2) expanded 14.2percent in the review period compared to the growth of 8.9percent in the corresponding period of the previous year. On y-o-y basis, M2 expanded 23.9percent in mid-April 2021.

  • Domestic credit increased 16.4percent in the review period compared to a growth of 9.1percent in the corresponding period of the previous year. On y-o-y basis, domestic credit increased 21.1percent in mid-April 2021.

  • Deposits at Banks and Financial Institutions (BFIs) increased 13.7 percent in the review period compared to a growth of 9.7 percent in the corresponding period of the previous year. On y-o-y basis, deposits at BFIs expanded 23.0 percent in mid-March 2021.

  • Private sector credit from BFIs increased 22.5percent in the review period compared to a growth of 11.5percent in the corresponding period of previous year. On y-o-y basis, credit to the private sector from BFIs increased 23.1percent in mid-April 2021.

  • During the review period, NRB mopped up Rs.303.29 billion liquidity of which Rs.109.54 billion was through reverse repo auction and Rs.193.75 billion through deposit collection. In the review period, NRB injected liquidity of Rs.1.02 billion through repo and Rs.7.12 billion through standing liquidity facility (SLF). In the corresponding period of the previous year, Rs.58 billion liquidity mopped up through reverse repo auction of Rs.28 billion and deposit collection of Rs.30 billion.

  • Outstanding amount of refinance provided by NRB remained Rs.123.14 billion in mid-April 2021to 48860 borrowers.

  • Weighted average deposit rate and lending rate of commercial banks stood at 4.79 percent and 8.61 percent respectively in the review month. Such rates were 6.74 percent and 11.77 percent respectively a year ago.

  • NEPSE index stood at 2714.8 in mid-April2021compared to 1251.5in mid-April2020. Such an index was 1362.4 in mid-July 2020.57.

  • Stock market capitalization in mid-April2021 stood Rs.3758.98 billion compared to Rs.1792.76 billion in mid-July 2020.

     

    Permanent Source : here