```#R individual assignment Q2 head(pwt)
library(dplyr) library(ggplot2)
library(dplyr) library(ggplot2)
pwt <- pwt %>% mutate(gdp_per_capita = rgdpe / pop) # Real GDP per capita (PPP-adjusted)
selected_countries <- c(“Pakistan”, “China”, “India”,“Bangladesh”) selected_data <- pwt %>% filter(country %in% selected_countries & year >= 1989 & year <= 2010) # Filter data for countries and years
ggplot(selected_data, aes(x = year, y = gdp_per_capita, color = country)) + geom_line(size = 1) + labs(title = “GDP Per Capita Over Time (1989-2010)”, x = “Year”, y = “GDP Per Capita (PPP-adjusted, in million 2010 US$)”, color = “Country”) + theme_minimal() + theme(legend.position = “bottom”)
pwt <- pwt %>% mutate(labor_productivity = rgdpo / emp, capital_intensity = ck / emp)
selected_data <- pwt %>% filter(year >= 1989 & year <=2010)%>%filter(country==selected_countries) selected_data
#line plot ggplot(selected_data, aes(x = year, group = country)) + geom_line(aes(y = labor_productivity, color = “Labor Productivity”), size = 1) + geom_line(aes(y = capital_intensity, color = “Capital Intensity”), size = 1, linetype = “dashed”) + facet_wrap(~country, scales = “free”) + labs(title = “Labor Productivity and Capital Intensity Over Time (1989-2010)”, x = “Year”, y = “Value”, color = “Metric”) + theme_minimal()
#heatmap
selected_data <- selected_data %>% group_by(country) %>% mutate(avg_labor_productivity = mean(labor_productivity, na.rm = TRUE)) %>% ungroup() %>% mutate(country = reorder(country, avg_labor_productivity))
ggplot(selected_data, aes(x = year, y = country, fill = labor_productivity)) + geom_tile() + scale_fill_gradient(low = “yellow”, high = “red”, name = “Labor Productivity”) + labs(title = “Labor Productivity by Country and Year (1989-2010)”, x = “Year”, y = “Country (Sorted by Avg. Productivity)”) + theme_minimal() + theme(axis.text.y = element_text(size = 8))
####GDP Components
library(dplyr) library(ggplot2) install.packages(“tidyr”) library(tidyr)
pwt is your datasetselected_countries with specific countries or
regions (e.g., G7, BRICS, etc.)selected_data <- pwt %>% filter(year >= 1989 & year <= 2010) %>% # Filter for years of interest filter(country %in% selected_countries) %>% # Filter for selected countries select(country, year, ccon, cda, ctfp) # Select relevant variables
gdp_data <- selected_data %>% gather(key = “component”, value = “value”, ccon, cda) # Convert wide to long format
ggplot(gdp_data, aes(x = year, y = value, fill = component)) + geom_area(alpha = 0.8) + facet_wrap(~ country, scales = “free_y”) + # Separate plots for each country labs(title = “Breakdown of GDP Components (1989-2010)”, x = “Year”, y = “GDP Components (in million 2010 US$)”, fill = “Component”) + scale_fill_manual(values = c(“ccon” = “blue”, “cda” = “orange”), labels = c(“Consumption”, “Investment”)) + theme_minimal() + theme(legend.position = “bottom”)
selected_countries with a list of countries to
analyzeselected_data <- pwt %>% filter(year >= 1989 & year <= 2010) %>% # Filter for years of interest filter(country %in% selected_countries) %>% # Filter for selected countries mutate(capital_intensity = ck / emp) %>% # Calculate capital intensity select(country, year, capital_intensity, ck, cn, emp) # Select relevant variables
ggplot(selected_data, aes(x = year, y = capital_intensity, color = country)) + geom_line(size = 1) + labs(title = “Capital Intensity Over Time (1989-2010)”, x = “Year”, y = “Capital Intensity (Capital Stock per Worker)”, color = “Country”) + theme_minimal() + theme(legend.position = “bottom”)
average_capital_data <- selected_data %>% group_by(country) %>% summarise(avg_capital_stock = mean(cn, na.rm = TRUE), avg_employment = mean(emp, na.rm = TRUE))
ggplot(average_capital_data, aes(x = reorder(country, avg_capital_stock), y = avg_capital_stock, fill = country)) + geom_bar(stat = “identity”) + coord_flip() + # Flip coordinates for better readability labs(title = “Average Capital Stock by Country (1989-2010)”, x = “Country”, y = “Average Capital Stock (in million 2010 US$)”) + scale_fill_viridis_d() + theme_minimal() + theme(legend.position = “none”)
#population
library(dplyr) library(ggplot2)
selected_countries with the list of countries
to analyzeselected_data <- pwt %>% filter(year >= 1990 & year <= 2017) %>% # Filter for years of interest filter(country %in% selected_countries) %>% # Filter for selected countries select(country, year, pop, emp, avh, rgdpo) %>% # Select relevant variables mutate(labor_force_participation = (emp / pop) * 100, # Calculate labor force participation rate (%) labor_productivity = rgdpo / emp) selected_data
average_participation <- selected_data %>% group_by(country) %>% summarise(avg_lfp = mean(labor_force_participation, na.rm = TRUE))
ggplot(average_participation, aes(x = reorder(country, -avg_lfp), y = avg_lfp, fill = country)) + geom_bar(stat = “identity”) + coord_flip() + labs(title = “Average Labor Force Participation Rate (1989-2010)”, x = “Country”, y = “Labor Force Participation Rate (%)”) + scale_fill_viridis_d() + theme_minimal() + theme(legend.position = “none”)
library(DT)
selected_data <- pwt %>% filter(year >= 1989 & year <= 2010) %>% # Filter for years of interest filter(country %in% selected_countries) %>% # Filter for selected countries select(country, year, pop, emp, avh, hc) # Create an interactive table datatable(selected_data, options = list(pageLength = 15, autoWidth = TRUE))
```