---
title: "Geoms in lemon"
author: "Stefan McKinnon Edwards <sme@iysik.com>"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    fig_caption: yes
vignette: >
  %\VignetteIndexEntry{Geoms in lemon}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup,include=FALSE}
library(knitr)

knitr::opts_chunk$set(fig.height=4, fig.width=6,
                      cache=TRUE, autodep = TRUE, cache.path = 'geoms-cache/')
```

**Deprecation notice:** The `geom_pointline and geom_pointpath` have been deprecated in lemon v. 0.5.0. Instead, we refer to the similar geom in [ggh4x](https://teunbrand.github.io/ggh4x/index.html).

In this vignette, we will demonstrate the two geoms: **geom_pointpath** and **geom_siderange**.

## geom_pointpath

`geom_pointline` simply combines `geom_point` and `geom_line`, while `geom_pointpath` combines `geom_point` and `geom_path`. The difference is that `geom_line` draws its lines through the data points, `geom_pointline` leaves a small aesthetic gap between the symbol and the line:

```{r fig.height=8,fig.cap=''}
library(ggplot2)
library(ggh4x)
library(gridExtra)

data(sunspot.year)
sunspots <- data.frame(count = as.numeric(sunspot.year), year = seq.int(start(sunspot.year)[1], end(sunspot.year)[1]))
sunspots <- subset(sunspots, year > 1900)

point <-  ggplot(sunspots, aes(x = year, y = count)) + geom_point() + geom_line() + labs(title = "geom_point + geom_line")
pointline <- ggplot(sunspots, aes(x = year, y = count)) + geom_pointpath(mult = 0.4) + labs(title = 'ggh4x geom_pointline')
grid.arrange(point, pointline)
```

### lemon and ggh4x

The geom `geom_pointpath` in ggh4x can be used as replacement for both lemon's `geom_pointline` and `geom_pointpath`:

+----------------------------------+----------------------------------------------------------------------------+
| lemon                            | ggh4x                                                                      |
+==================================+============================================================================+
| `geom_pointpath(distance = ...)` | `geom_pointpath(mult = ...)` \|                                            |
|                                  |                                                                            |
| `geom_pointline(distance = ...)` | The `mult` argument is a numeric value to scale the proportion of the gap. |
+----------------------------------+----------------------------------------------------------------------------+

## geom_siderange

The `geom_siderange` projects data onto the horizontal or vertical edge of the panels.

```{r fig.height=4,fig.cap=''}
library(lemon)
x <- rnorm(25)
df <- data.frame(x = x, y = x + rnorm(25, sd = 0.2), 
                 a = sample(c("horse", "goat"), 25, replace = TRUE), 
                 stringsAsFactors = FALSE)
df$y <- with(df, ifelse(y > 1 & a == 'horse', 1, y))
p <- ggplot(df, aes(x = x, y = y, colour = a)) + geom_point(shape = 1)

p + geom_siderange(start = 19)
```


Capping the sideranges with different symbols:
```{r fig.height=4,fig.cap=''}
p + geom_siderange(start = 19, end = 22, fill='black', sides = 'b') +
  geom_siderange(sides = 'tl')
```

It also works with facets:
```{r fig.height=4,fig.cap=''}
p <- ggplot(mpg, aes(displ, hwy, colour = fl)) +
  geom_point() +
  facet_wrap(~class, nrow = 4)

p + geom_siderange()
```
