https://jjchern.github.io/gglorenz/
Plotting Lorenz curves with the blessing of ggplot2
https://jjchern.github.io/gglorenz/
ggplot-extension ggplot2
Last synced: 13 days ago
JSON representation
Plotting Lorenz curves with the blessing of ggplot2
- Host: GitHub
- URL: https://jjchern.github.io/gglorenz/
- Owner: jjchern
- License: other
- Created: 2018-02-08T20:33:23.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-13T00:58:50.000Z (over 5 years ago)
- Last Synced: 2025-10-22T04:39:06.735Z (about 2 months ago)
- Topics: ggplot-extension, ggplot2
- Language: R
- Homepage: https://jjchern.github.io/gglorenz/
- Size: 1.94 MB
- Stars: 30
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- awesome-ggplot2 - gglorenz
README
---
output: github_document
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE
)
```
[](https://CRAN.R-project.org/package=gglorenz)
[](https://travis-ci.org/jjchern/gglorenz)
[](https://ci.appveyor.com/project/jjchern/gglorenz)
# 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)