Explore the National Pension Portfolio and the 2024 Funding Status with R Visualization (feat. Stacked Bar Graph)

Nowadays National Insurance You've heard it all before: the news is stoking fears that the fund could run out of money by 2055, and some people are questioning the government's involvement in pension funds. How much of this information can you trust?

So today we're going to spend some time analyzing the "National Pension Portfolio" data ourselves.
And specifically R programming languageto create a stacked bar graph visualization. Visual datais different: while visualizing data, the National Pension's asset structure and changes over the yearsat a glance.

What is a National Insurance portfolio?

The National Pension Fund portfolio is simply a mix of assets that shows how the fund is investing its money. It invests in a variety of assets, including domestic and international equities, bonds, alternative investments, and real estate. In recent years, the fund has been increasing its allocation to riskier assets and diversifying its investments. However, improving returns and delaying the time to fund depletion remain key challenges.

Stacked bar graphs in R: See the composition of National Insurance assets

Now let's visualize the National Insurance portfolio data. First, we'll look at the asset mix via a stacked bar graph.

R code and description

# Install and load necessary packages
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
if (!requireNamespace("reshape2", quietly = TRUE)) install.packages("reshape2")
library(ggplot2)
library(reshape2)

# Create data
portfolio_data <- data.frame(
    Year = c("2020", "2021", "2022", "2023", "2024"),
    Domestic_Stock = c(148, 160, 170, 185, 200),
    Foreign_Stock = c(351, 370, 390, 410, 430),
    Bond = c(320, 310, 300, 290, 280),
    Alternative = c(171, 180, 190, 200, 210)
)

# Transform data
melted_data <- melt(portfolio_data, id.vars = "Year")
names(melted_data)[names(melted_data) == "variable"] <- "Asset_Type"

# Calculate totals separately
totals <- aggregate(value ~ Year, melted_data, sum)
names(totals) <- c("Year", "Total")

# Calculate percentages
melted_data$percentage <- ave(melted_data$value,
                              melted_data$Year,
                              FUN = function(x) x / sum(x) * 100)

# Create plot
ggplot(melted_data, aes(x = Year, y = value)) +
    geom_bar(aes(fill = Asset_Type), stat = "identity") +
    geom_text(aes(label = sprintf("%d\n(%.1f%%)", value, percentage)),
              position = position_stack(vjust = 0.5),
              color = "white",
              size = 3)
    geom_text(data = totals,
              aes(y = Total, label = Total),
              position = position_stack(vjust = 1.05),
              color = "black",
              fontface = "bold") +
    labs(title = "National Pension Fund Asset Composition",
         x = "Year",
         y = "Asset Value (Trillion KRW)",
         fill = "Asset Type") +
    theme_minimal()
연도별 국민연금 포트폴리오
(National Insurance portfolio by year)

R Code Explained in Detail

1. Install and load the package

if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
if (!requireNamespace("reshape2", quietly = TRUE)) install.packages("reshape2")
library(ggplot2)
library(reshape2)
  • requireNamespace()to verify that the required packages are installed
  • If the package doesn't exist, install it automatically
  • ggplot2: Packages for data visualization
  • reshape2: Packages for converting data structures

2. generate data

portfolio_data <- data.frame(
    Year = c("2020", "2021", "2022", "2023", "2024"),
    Domestic_Stock = c(148, 160, 170, 185, 200),
    Foreign_Stock = c(351, 370, 390, 410, 430),
    Bond = c(320, 310, 300, 290, 280),
    Alternative = c(171, 180, 190, 200, 210)
)
  • data.frame Create
  • Includes amount data for each asset type by year
  • Column structure: Year and 4 asset types

3. transform your data

melted_data <- melt(portfolio_data, id.vars = "Year")
names(melted_data)[names(melted_data) == "variable"] <- "Asset_Type"
  • melt() Convert from wide format to long format using functions
  • id.vars = "Year"to specify the Year column as an identifier
  • Renamed variable from 'variable' to 'Asset_Type' to clarify meaning

4. calculate the total

totals <- aggregate(value ~ Year, melted_data, sum)
names(totals) <- c("Year", "Total")
  • aggregate() Use functions to calculate totals by year
  • Name the columns in the resulting dataframe Year and Total

5. Calculate the ratio

melted_data$percentage <- ave(melted_data$value,
                            melted_data$Year,
                            FUN = function(x) x / sum(x) * 100)
  • ave() Calculate asset ratios for each year using functions
  • Divide each value by the total for the year and multiply by 100 to calculate the percentage

6. Create a graph

ggplot(melted_data, aes(x = Year, y = value)) +
    geom_bar(aes(fill = Asset_Type), stat = "identity") +
    geom_text(aes(label = sprintf("%d\n(%.1f%%)", value, percentage)),
              position = position_stack(vjust = 0.5),
              color = "white",
              size = 3)
    geom_text(data = totals,
              aes(y = Total, label = Total),
              position = position_stack(vjust = 1.05),
              color = "black",
              fontface = "bold") +
    labs(title = "National Pension Fund Asset Composition",
         x = "Year",
         y = "Asset Value (Trillion KRW)",
         fill = "Asset Type") +
    theme_minimal()
  • ggplot(): Create a basic graph object
  • geom_bar(): Create a bar graph
  • stat = "identity": use actual value as is
  • fill = Asset_Type: Color-coded by asset type
  • The first geom_text(): Show values and percentages in each bar section
  • sprintf(): Formatting text
  • position_stack(): Adjust text position
  • The second geom_text(): Show total by year
  • vjust = 1.05: Place text above the bar
  • fontface = "bold": Apply boldface
  • labs(): Set graph title and axis labels
  • theme_minimal(): Apply a clean theme

National Pension Independence and Reform: Why is it Important?

"The independence and reform of the national pension system" has become a very important topic amid concerns about the depletion of the national pension fund.
Several changes are needed to ensure that the National Pension System is stable and free from political interference.

국민연금 필요한 변화 그림
(National Insurance required changes)

Necessary changes

  1. Increase operational independence: Separate the Fund from government influence and create an expert-driven decision-making structure.
  2. Expand transparency: Regular reporting and disclosure of the fund's status should be made public so that the public can easily understand it.
  3. Diversify your investments: Strengthen global investments and stable asset allocation to manage risk and improve returns.

National Insurance portfolios, a data-driven view

Today we analyzed a national pension portfolio using R visualizations. The data gives us an objective view of the asset composition of the national pension and the growth of assets over the years through stacked bar graphs.

We've also highlighted why independence and reform of the National Insurance system is so important - now it's time to add your voice! Your participation can change our future!

Similar Posts