Create a data frame
Type <- c("A", "B", "C")
Percentage <- c(50, 30, 20)
type_df <- data.frame(Type, Percentage)
type_df
## Type Percentage
## 1 A 50
## 2 B 30
## 3 C 20
Plot the stacked column chart
library(ggplot2)
stacked_chart <- ggplot(type_df) + aes(x = "", y = Percentage, fill = Type) +
geom_col(width = 0.5) +
geom_text(aes(label = Percentage),
position = position_stack(vjust = 0.5))
stacked_chart
Use the stacked chart created to do a pie chart
Load the data We will use the country data
## X country continent year lifeExp pop gdpPercap
## 1 1 Afghanistan Asia 1952 28.801 8425333 779.4453
## 2 2 Afghanistan Asia 1957 30.332 9240934 820.8530
## 3 3 Afghanistan Asia 1962 31.997 10267083 853.1007
## 4 4 Afghanistan Asia 1967 34.020 11537966 836.1971
## 5 5 Afghanistan Asia 1972 36.088 13079460 739.9811
## 6 6 Afghanistan Asia 1977 38.438 14880372 786.1134
##
## [,1]
## Afghanistan 12
## Albania 12
## Algeria 12
## Angola 12
## Argentina 12
## Australia 12
## Austria 12
## Bahrain 12
## Bangladesh 12
## Belgium 12
## Benin 12
## Bolivia 12
## Bosnia and Herzegovina 12
## Botswana 12
## Brazil 12
## Bulgaria 12
## Burkina Faso 12
## Burundi 12
## Cambodia 12
## Cameroon 12
## Canada 12
## Central African Republic 12
## Chad 12
## Chile 12
## China 12
## Colombia 12
## Comoros 12
## Congo, Dem. Rep. 12
## Congo, Rep. 12
## Costa Rica 12
## Cote d'Ivoire 12
## Croatia 12
## Cuba 12
## Czech Republic 12
## Denmark 12
## Djibouti 12
## Dominican Republic 12
## Ecuador 12
## Egypt 12
## El Salvador 12
## Equatorial Guinea 12
## Eritrea 12
## Ethiopia 12
## Finland 12
## France 12
## Gabon 12
## Gambia 12
## Germany 12
## Ghana 12
## Greece 12
## Guatemala 12
## Guinea 12
## Guinea-Bissau 12
## Haiti 12
## Honduras 12
## Hong Kong, China 12
## Hungary 12
## Iceland 12
## India 12
## Indonesia 12
## Iran 12
## Iraq 12
## Ireland 12
## Iron City 1
## Israel 12
## Italy 12
## Jamaica 12
## Japan 12
## Jordan 12
## Kenya 12
## Korea, Dem. Rep. 12
## Korea, Rep. 12
## Kuwait 12
## Lebanon 12
## Lesotho 12
## Liberia 12
## Libya 12
## Madagascar 12
## Malawi 12
## Malaysia 12
## Mali 12
## Mauritania 12
## Mauritius 12
## Mexico 12
## Mongolia 12
## Montenegro 12
## Morocco 12
## Mozambique 12
## Myanmar 12
## Namibia 12
## Nepal 12
## Netherlands 12
## New Zealand 12
## Nicaragua 12
## Niger 12
## Nigeria 12
## Norway 12
## Oman 12
## Pakistan 12
## Panama 12
## Paraguay 12
## Peru 12
## Philippines 12
## Poland 12
## Portugal 12
## Puerto Rico 12
## Reunion 12
## Romania 12
## Rwanda 12
## Sao Tome and Principe 12
## Saudi Arabia 12
## Senegal 12
## Serbia 12
## Sierra Leone 12
## Singapore 12
## Slovak Republic 12
## Slovenia 12
## Somalia 12
## South Africa 12
## Spain 12
## Sri Lanka 12
## Sudan 12
## Swaziland 12
## Sweden 12
## Switzerland 12
## Syria 12
## Taiwan 12
## Tanzania 12
## Thailand 12
## Togo 12
## Trinidad and Tobago 12
## Tunisia 12
## Turkey 12
## Uganda 12
## United Kingdom 12
## United States 12
## Uruguay 12
## Venezuela 12
## Vietnam 12
## West Bank and Gaza 12
## Yemen, Rep. 12
## Zalem 1
## Zambia 12
## Zimbabwe 12
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
##
## Africa Americas Asia Europe Oceania
## 624 302 396 360 24
## [1] "X" "country" "continent" "year" "lifeExp" "pop"
## [7] "gdpPercap"
Get percentage frequencies
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
## country_df$continent n percent
## Africa 624 0.3657679
## Americas 302 0.1770223
## Asia 396 0.2321219
## Europe 360 0.2110199
## Oceania 24 0.0140680
## [1] "data.frame"
## Continent Number Percentage
## 1 Africa 624 0.3657679
## 2 Americas 302 0.1770223
## 3 Asia 396 0.2321219
## 4 Europe 360 0.2110199
## 5 Oceania 24 0.0140680
## Continent Number Percentage
## 1 Africa 624 36.58
## 2 Americas 302 17.70
## 3 Asia 396 23.21
## 4 Europe 360 21.10
## 5 Oceania 24 1.41
Create a stacked chart using the country data
stacked_chart_continent <- ggplot(continent_df) + aes(x = "", y = Percentage, fill = Continent) +
geom_col(width = 0.5) +
geom_text(aes(label = Percentage),
position = position_stack(vjust = 0.5)) +
ggtitle("Proportion of Records") + xlab("")
stacked_chart_continent
Create a pie stacked chart using the country data
pie_chart_continent <- stacked_chart_continent + coord_polar("y", start=0) +
xlab("")
pie_chart_continent
pie_chart_continent_grey <- stacked_chart_continent + coord_polar("y", start=0) +
xlab("") + scale_fill_grey()
pie_chart_continent_grey
Create a histogram using the country data
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Kurtosis and Skewness
## [1] 1.873037
## [1] -0.2513104
A histogram of life expectancy
ggplot(country_df) + aes(lifeExp) +
geom_histogram(fill = "Orange", colour = "blue") +
xlab("Life Expectancy") +
ylab("Count") +
ggtitle("Histogram of Life Expectancy, 1952 - 2007") +
annotate("text", x = 40, y = 150, label = "Kurtosis = 1.87") +
annotate("text", x = 40, y = 130, label = "Skewness = -0.25")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Label the data points
## [1] "ID" "country" "continent" "year" "lifeExp" "pop"
## [7] "gdpPercap"
## ID country continent year lifeExp pop gdpPercap
## 1 1705 Iron City Americas 2007 50.123 6666666 35666.42
## ID country continent year lifeExp pop gdpPercap
## 1 1706 Zalem Americas 2007 80.123 666 10666.5
ggplot(country_df) + aes(lifeExp) +
geom_histogram(fill = "Orange", colour = "blue") +
xlab("Life Expectancy") +
ylab("Count") +
ggtitle("Histogram of Life Expectancy, 1952 - 2007") +
annotate("text", x = 40, y = 150, label = "Kurtosis = 1.87") +
annotate("text", x = 40, y = 130, label = "Skewness = -0.25") +
geom_vline(xintercept = 50.123, linetype = "dashed", color = "red") +
annotate("text", x = 36, y = 100, label = "Iron City's Life Expectancy \n is 50 years old")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Filter for specific records. In this case, we will explore Alita’s worlds, Iron City and Zalem.
##
## Africa Americas Asia Europe Oceania
## 624 302 396 360 24
## [1] "ID" "country" "continent" "year" "lifeExp" "pop"
## [7] "gdpPercap"
## ID country continent year lifeExp pop gdpPercap
## 1 1705 Iron City Americas 2007 50.123 6666666 35666.42
## ID country continent year lifeExp pop gdpPercap
## 1 1706 Zalem Americas 2007 80.123 666 10666.5
Calculate the mean and median
## [1] 29570418
## [1] 7021053
R does not have a mode function, so we will create one.
Calculate the mode using the mode function.
## [1] Africa
## Levels: Africa Americas Asia Europe Oceania
Pivot tables are very useful to explore the data and get aggregates
Mean population by continent
## continent pop
## 1 Africa 9916003
## 2 Americas 24364589
## 3 Asia 77038722
## 4 Europe 17169765
## 5 Oceania 8874672
Median population by continent
## continent pop
## 1 Africa 4579311
## 2 Americas 6227510
## 3 Asia 14530831
## 4 Europe 8551125
## 5 Oceania 6403492
Mean population by continent and year
## continent year pop
## 1 Africa 1952 4570010
## 2 Americas 1952 13806098
## 3 Asia 1952 42283556
## 4 Europe 1952 13937362
## 5 Oceania 1952 5343003
## 6 Africa 1957 5093033
## 7 Americas 1957 15478157
## 8 Asia 1957 47356988
## 9 Europe 1957 14596345
## 10 Oceania 1957 5970988
## 11 Africa 1962 5702247
## 12 Americas 1962 17330810
## 13 Asia 1962 51404763
## 14 Europe 1962 15345172
## 15 Oceania 1962 6641759
## 16 Africa 1967 6447875
## 17 Americas 1967 19229865
## 18 Asia 1967 57747361
## 19 Europe 1967 16039299
## 20 Oceania 1967 7300207
## 21 Africa 1972 7305376
## 22 Americas 1972 21175368
## 23 Asia 1972 65180977
## 24 Europe 1972 16687835
## 25 Oceania 1972 8053050
## 26 Africa 1977 8328097
## 27 Americas 1977 23122708
## 28 Asia 1977 72257987
## 29 Europe 1977 17238818
## 30 Oceania 1977 8619500
## 31 Africa 1982 9602857
## 32 Americas 1982 25211637
## 33 Asia 1982 79095018
## 34 Europe 1982 17708897
## 35 Oceania 1982 9197425
## 36 Africa 1987 11054502
## 37 Americas 1987 27310159
## 38 Asia 1987 87006690
## 39 Europe 1987 18103139
## 40 Oceania 1987 9787208
## 41 Africa 1992 12674645
## 42 Americas 1992 29570964
## 43 Asia 1992 94948248
## 44 Europe 1992 18604760
## 45 Oceania 1992 10459826
## 46 Africa 1997 14304480
## 47 Americas 1997 31876016
## 48 Asia 1997 102523803
## 49 Europe 1997 18964805
## 50 Oceania 1997 11120715
## 51 Africa 2002 16033152
## 52 Americas 2002 33990910
## 53 Asia 2002 109145521
## 54 Europe 2002 19274129
## 55 Oceania 2002 11727415
## 56 Africa 2007 17875763
## 57 Americas 2007 33538464
## 58 Asia 2007 115513752
## 59 Europe 2007 19536618
## 60 Oceania 2007 12274974
Median population by continent and year
## continent year pop
## 1 Africa 1952 2668125
## 2 Americas 1952 3146381
## 3 Asia 1952 7982342
## 4 Europe 1952 7199787
## 5 Oceania 1952 5343003
## 6 Africa 1957 2885791
## 7 Americas 1957 3507701
## 8 Asia 1957 9128546
## 9 Europe 1957 7507528
## 10 Oceania 1957 5970988
## 11 Africa 1962 3145210
## 12 Americas 1962 3880130
## 13 Asia 1962 10267083
## 14 Europe 1962 7814503
## 15 Oceania 1962 6641759
## 16 Africa 1967 3473693
## 17 Americas 1967 4318137
## 18 Asia 1967 11261690
## 19 Europe 1967 8140724
## 20 Oceania 1967 7300207
## 21 Africa 1972 3945595
## 22 Americas 1972 4698301
## 23 Asia 1972 12412593
## 24 Europe 1972 8444744
## 25 Oceania 1972 8053050
## 26 Africa 1977 4522666
## 27 Americas 1977 5302800
## 28 Asia 1977 13933198
## 29 Europe 1977 8741695
## 30 Oceania 1977 8619500
## 31 Africa 1982 5668229
## 32 Americas 1982 5968349
## 33 Asia 1982 14441916
## 34 Europe 1982 8962461
## 35 Oceania 1982 9197425
## 36 Africa 1987 6635612
## 37 Americas 1987 6655297
## 38 Asia 1987 16495304
## 39 Europe 1987 9101371
## 40 Oceania 1987 9787208
## 41 Africa 1992 7140389
## 42 Americas 1992 7351181
## 43 Asia 1992 17861905
## 44 Europe 1992 9272632
## 45 Oceania 1992 10459826
## 46 Africa 1997 7805423
## 47 Americas 1997 7992357
## 48 Asia 1997 21229759
## 49 Europe 1997 9527017
## 50 Oceania 1997 11120715
## 51 Africa 2002 8821779
## 52 Americas 2002 8650322
## 53 Asia 2002 22662365
## 54 Europe 2002 9518744
## 55 Oceania 2002 11727415
## 56 Africa 2007 10093311
## 57 Americas 2007 9119152
## 58 Asia 2007 24821286
## 59 Europe 2007 9493598
## 60 Oceania 2007 12274974