-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
138 lines (116 loc) · 5.13 KB
/
README.Rmd
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
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE
)
```
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/gglorenz)](https://CRAN.R-project.org/package=gglorenz)
[![Travis-CI Build Status](https://travis-ci.org/jjchern/gglorenz.svg?branch=master)](https://travis-ci.org/jjchern/gglorenz)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/jjchern/gglorenz?branch=master&svg=true)](https://ci.appveyor.com/project/jjchern/gglorenz)
<!-- badges: end -->
# About `gglorenz`
The goal of `gglorenz` is to plot Lorenz Curves with the blessing of `ggplot2`.
# Installation
```R
# Install the CRAN version
install.packages("gglorenz")
# Install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("jjchern/gglorenz")
```
# Example
Suppose you have a vector with each element representing the amount of income or wealth of an individual, and you are interested in knowing how much of that is produced by the top x% of the population, then the `gglorenz::stat_lorenz(desc = TRUE)` would make a ggplot2 graph for you.
```{r}
library(tidyverse)
library(gglorenz)
billionaires
billionaires %>%
ggplot(aes(TNW)) +
stat_lorenz(desc = TRUE) +
coord_fixed() +
geom_abline(linetype = "dashed") +
theme_minimal() +
hrbrthemes::scale_x_percent() +
hrbrthemes::scale_y_percent() +
hrbrthemes::theme_ipsum_rc() +
labs(x = "Cumulative Percentage of the Top 500 Billionaires",
y = "Cumulative Percentage of Total Net Worth",
title = "Inequality Among Billionaires",
caption = "Source: https://www.bloomberg.com/billionaires/ (accessed February 8, 2018)")
billionaires %>%
filter(Industry %in% c("Technology", "Real Estate")) %>%
ggplot(aes(x = TNW, colour = Industry)) +
stat_lorenz(desc = TRUE) +
coord_fixed() +
geom_abline(linetype = "dashed") +
theme_minimal() +
hrbrthemes::scale_x_percent() +
hrbrthemes::scale_y_percent() +
hrbrthemes::theme_ipsum_rc() +
labs(x = "Cumulative Percentage of Billionaires",
y = "Cumulative Percentage of Total Net Worth",
title = "Real Estate is a Relatively Equal Field",
caption = "Source: https://www.bloomberg.com/billionaires/ (accessed February 8, 2018)")
```
If you have a data frame with columns indicating the wealth and number of individuals at that level you can use the `n` aesthetic like so: `ggplot(freqdata, aes(x = value, n = freq) + stat_lorenz()`.
In addition, the `annotate_ineq()` function allows you to label the chart with inequality statistics such as the Gini coefficient:
```{r}
billionaires %>%
ggplot(aes(TNW)) +
stat_lorenz(desc = TRUE) +
coord_fixed() +
geom_abline(linetype = "dashed") +
theme_minimal() +
hrbrthemes::scale_x_percent() +
hrbrthemes::scale_y_percent() +
hrbrthemes::theme_ipsum_rc() +
labs(x = "Cumulative Percentage of the Top 500 Billionaires",
y = "Cumulative Percentage of Total Net Worth",
title = "Inequality Among Billionaires",
caption = "Source: https://www.bloomberg.com/billionaires/ (accessed February 8, 2018)") +
annotate_ineq(billionaires$TNW)
```
You can also use other geoms such as `area` or `polygon` and arranging population in ascending order:
```{r}
billionaires %>%
filter(Industry %in% c("Technology", "Real Estate")) %>%
add_row(Industry = "Perfect Equality", TNW = 1) %>%
ggplot(aes(x = TNW, fill = Industry)) +
stat_lorenz(geom = "area", alpha = 0.65) +
coord_fixed() +
hrbrthemes::scale_x_percent() +
hrbrthemes::scale_y_percent() +
hrbrthemes::theme_ipsum_rc() +
theme(legend.title = element_blank()) +
labs(x = "Cumulative Percentage of Billionaires",
y = "Cumulative Percentage of Total Net Worth",
title = "Real Estate is a Relatively Equal Field",
caption = "Source: https://www.bloomberg.com/billionaires/ (accessed February 8, 2018)")
billionaires %>%
filter(Industry %in% c("Technology", "Real Estate")) %>%
mutate(Industry = forcats::as_factor(Industry)) %>%
ggplot(aes(x = TNW, fill = Industry)) +
stat_lorenz(geom = "polygon", alpha = 0.65) +
geom_abline(linetype = "dashed") +
coord_fixed() +
hrbrthemes::scale_x_percent() +
hrbrthemes::scale_y_percent() +
hrbrthemes::theme_ipsum_rc() +
theme(legend.title = element_blank()) +
labs(x = "Cumulative Percentage of Billionaires",
y = "Cumulative Percentage of Total Net Worth",
title = "Real Estate is a Relatively Equal Field",
caption = "Source: https://www.bloomberg.com/billionaires/ (accessed February 8, 2018)")
```
# Acknowledgement
The package came to exist solely because Bob Rudis was [generous enough](https://github.com/hrbrmstr/ggalt/issues/46) to write a chapter that [demystifies `ggplot2`.](https://rud.is/books/creating-ggplot2-extensions/demystifying-ggplot2.html)