-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountry_v_World.qmd
155 lines (125 loc) · 4.48 KB
/
Country_v_World.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: "2022 Index of Economic Freedom"
subtitle: ""
author: "Felipe Valencia"
format:
html:
self-contained: true
page-layout: full
title-block-banner: true
toc: true
toc-depth: 3
toc-location: body
number-sections: false
html-math-method: katex
code-fold: true
code-summary: "Show the code"
code-overflow: wrap
code-copy: hover
code-tools:
source: false
toggle: true
caption: See code
---
## Background
_Background or Elevator Pitch goes here_
## Data Wrangling
_Data wrangling process goes here_
```{python}
#| label: libraries
#| include: false
# Import libraries
import pandas as pd
import altair as alt
import numpy as np
import sqlite3
from IPython.display import Markdown
from IPython.display import display
from tabulate import tabulate
# Handle Limit size
alt.data_transformers.disable_max_rows()
```
```{python}
#| label: Read CSV file
#| code-summary: Read and format data
# Loading in data:
url = "https://raw.githubusercontent.com/felipevalenciaclavijo/EconomicFreedomIndex/main/index2022_data.csv"
data = pd.read_csv(url, encoding_errors='ignore')
```
```{python}
#| label: Data wrangling
#| code-summary: Data wrangling
# Filter to compare between observations: Countries, Regions, and World
# Input:
Observation_Name1 = 'Mexico'
Observation_Name2 = 'World'
# fix a blank space on column 'Country Name'
data = data.rename(columns = {'Country Name':'Country_Name'})
comparison_data = data.query('Country_Name == @Observation_Name1 | Country_Name == @Observation_Name2')
# Simplified data frame containing what I need
comparison_data_scores = comparison_data[['Country_Name', 'Property Rights', 'Judical Effectiveness', 'Government Integrity', 'Tax Burden', "Gov't Spending", 'Fiscal Health', 'Business Freedom', 'Labor Freedom', 'Monetary Freedom', 'Trade Freedom', 'Investment Freedom ', 'Financial Freedom']].reset_index()
# fix a blank space at the end of 'Investment Freedom '
comparison_data_scores = comparison_data_scores.rename(columns = {'Investment Freedom ':'Investment Freedom'})
# Transpose dataframe to desired way
comparison_data_scores = comparison_data_scores.transpose()
# Rename columns as desired
comparison_data_scores.columns = np.append(comparison_data_scores.iloc[1, :2], comparison_data_scores.columns[2:])
comparison_data_scores = comparison_data_scores.reset_index()
comparison_data_scores = comparison_data_scores.rename(columns = {'index':'Subcategories'})
# Filter to remove unnecessary rows
comparison_data_scores = comparison_data_scores.query('Subcategories != "index" & Subcategories != "Country_Name"')
# Tidy dataframe for data visualization purposes
comparison_data_scores = comparison_data_scores.melt(id_vars="Subcategories",
var_name="Country",
value_name="Score")
```
## Data Visualization
_Data Visualizations go here_
```{python}
#| label: Name for each label (has to be different)
#| code-summary: Summary goes here
# Determine colors
domain = [Observation_Name1, Observation_Name2]
range_ = ['#ff7f0e', 'steelblue']
# Plot grouped bar chart
comparison_with_world = (alt.Chart(comparison_data_scores).mark_bar()
.encode(
x=alt.X('Country:N', title='', axis=None),
y=alt.Y('Score:Q', scale=alt.Scale(domain=[0, 100])),
color=alt.Color('Country:N',title=None,
scale=alt.Scale(domain=domain, range=range_)))
.properties(
width=45,
height=200)
)
text = comparison_with_world.mark_text(
align='center',
baseline='middle',
dy=-7 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='Score:Q'
)
grouped_bar_chart= (alt.layer(comparison_with_world, text)
.transform_calculate(
Subcategories="split(datum.Subcategories, ' ')") # This wraps text
.facet(
column='Subcategories:N')
.properties(
title = {
"text": [f"2022 Index of Economic Freedom: {Observation_Name1} vs {Observation_Name2}"],
"subtitle": ["The Index of Economic Freedom is a score divided into 12 subcategories used to determine the degree of Economic Freedom of Nations", ""]
})
.configure_title(
fontSize = 18,
anchor = "start",
subtitleFontSize = 12)
.configure(font='arial')
)
grouped_bar_chart
# Save plot as a PNG
grouped_bar_chart.save(f'2022_EFI_{Observation_Name1}_vs_{Observation_Name2}.svg')
```
## Conclusions
_Conclusions go here_
## APPENDIX A (Additional Code)
_Aditional stuff goes here_