https://github.com/andrewheiss/reconplots
Convenient functions for plotting economics-style supply/demand curves in R
https://github.com/andrewheiss/reconplots
economics r r-package rstats
Last synced: 6 months ago
JSON representation
Convenient functions for plotting economics-style supply/demand curves in R
- Host: GitHub
- URL: https://github.com/andrewheiss/reconplots
- Owner: andrewheiss
- License: mit
- Created: 2017-09-17T05:48:59.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-19T17:54:34.000Z (about 8 years ago)
- Last Synced: 2025-04-10T01:00:54.685Z (6 months ago)
- Topics: economics, r, r-package, rstats
- Language: R
- Homepage:
- Size: 110 KB
- Stars: 38
- Watchers: 6
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---# reconPlots: Create economics-style plots with R
**Author:** [Andrew Heiss](https://www.andrewheiss.com/)
**License:** [MIT](https://opensource.org/licenses/MIT)[](https://travis-ci.org/andrewheiss/reconPlots)
[](https://codecov.io/github/andrewheiss/reconPlots?branch=master)```{r setup, echo=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "tools/README-",
message = FALSE
)
```Intro paragraph here.
## Installation
This package is not on CRAN yet.
You can install the development version from Github with [devtools](https://github.com/hadley/devtools):
```{r, eval=FALSE}
library(devtools)
install_github("andrewheiss/reconPlots")
```## Plotting intersections of curves
The `curve_intersect()` function calculates the intersection of two curves, defined as either as data frames with x and y columns, or as single-variable functions
### Straight lines with empirical data
```{r example-intersection}
library(reconPlots)line1 <- data.frame(x = c(1, 9), y = c(1, 9))
line1line2 <- data.frame(x = c(9, 1), y = c(1, 9))
line2line_intersection <- curve_intersect(line1, line2)
line_intersection
``````{r straight-line-intersection}
library(ggplot2)ggplot(mapping = aes(x = x, y = y)) +
geom_line(data = line1, color = "red", size = 1) +
geom_line(data = line2, color = "blue", size = 1) +
geom_vline(xintercept = line_intersection$x, linetype = "dotted") +
geom_hline(yintercept = line_intersection$y, linetype = "dotted") +
theme_classic()
```### Curved Bézier lines with empirical data
This also works with curved lines created with `Hmisc:bezier()`:
```{r curved-line-intersection}
curve1 <- data.frame(Hmisc::bezier(c(1, 8, 9), c(1, 5, 9)))
curve2 <- data.frame(Hmisc::bezier(c(1, 3, 9), c(9, 3, 1)))curve_intersection <- curve_intersect(curve1, curve2)
curve_intersectionggplot(mapping = aes(x = x, y = y)) +
geom_line(data = curve1, color = "red", size = 1) +
geom_line(data = curve2, color = "blue", size = 1) +
geom_vline(xintercept = curve_intersection$x, linetype = "dotted") +
geom_hline(yintercept = curve_intersection$y, linetype = "dotted") +
theme_classic()
```### Curved lines defined with functions
Instead of defining curves with empirical data (i.e. data frames of `x` and `y` values), you can also work with actual functions. The only change is that you need to set `empirical = FALSE` and define a range of values of x to look within for the intersection.
```{r curved-line-function-intersection}
curve1 <- function(q) (q - 10)^2
curve2 <- function(q) q^2 + 2*q + 8x_range <- 0:5
curve_intersection <- curve_intersect(curve1, curve2, empirical = FALSE,
domain = c(min(x_range), max(x_range)))ggplot() +
stat_function(aes(x_range), color = "blue", size = 1, fun = curve1) +
stat_function(aes(x_range), color = "red", size = 1, fun = curve2) +
geom_vline(xintercept = curve_intersection$x, linetype = "dotted") +
geom_hline(yintercept = curve_intersection$y, linetype = "dotted") +
theme_classic()
```