---
title: "tmap: charts"
output: 
  bookdown::html_vignette2:
pkgdown:
  as_is: true
template:
  math-rendering: mathjax
bibliography: '`r system.file("tmap.bib", package="tmap")`'
csl: "`r system.file('ieee.csl', package = 'tmap')`"
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  fig.width=6, 
  fig.height=3,
  comment = "#>"
)
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
   lines <- options$output.lines
   if (is.null(lines)) {
     return(hook_output(x, options))  # pass to default hook
   }
   x <- unlist(strsplit(x, "\n"))
   more <- "..."
   if (length(lines)==1) {        # first n lines
     if (length(x) > lines) {
       # truncate the output, but add ....
       x <- c(head(x, lines), more)
     }
   } else {
     x <- c(more, x[lines], more)
   }
   # paste these lines together
   x <- paste(c(x, ""), collapse = "\n")
   hook_output(x, options)
 })

```

```{r, echo = FALSE, message = FALSE}
library(tmap)
#devtools::load_all()
data(World, metro, rivers, land)
#tmap_design_mode()
```

## Charts

### Numerical variable

```{r}
tm_shape(World) + 
  tm_polygons("HPI", 
    fill.scale = tm_scale_intervals(), 
    fill.chart = tm_chart_histogram())
```

```{r}
tm_shape(World) + 
	tm_polygons("HPI", 
				fill.scale = tm_scale_continuous(), 
				fill.chart = tm_chart_histogram(
					position = tm_pos_out("center", "bottom"),
					width = 30)
				)
```

```{r}
tm_shape(World) + 
	tm_polygons("HPI", 
				fill.scale = tm_scale_intervals(), 
				fill.chart = tm_chart_donut())
```

```{r}
tm_shape(World) + 
	tm_polygons("HPI", 
				fill.scale = tm_scale_intervals(), 
				fill.chart = tm_chart_box())
```

```{r}
tm_shape(World) + 
	tm_polygons("HPI", 
				fill.scale = tm_scale_intervals(), 
				fill.chart = tm_chart_violin())
```

```{r}
# with additional ggplot2 code
require(ggplot2)
tm_shape(World) + 
	tm_polygons("HPI", 
				fill.scale = tm_scale_intervals(), 
				fill.chart = tm_chart_bar(
					extra.ggplot2 = theme(
						panel.grid.major.y = element_line(colour = "red")
					))
				)
```

```{r}
tm_shape(land) +
	tm_raster("trees",
			  col.chart = tm_chart_histogram())
```

### Categorical variable

```{r}
tm_shape(World) + 
	tm_polygons("economy", 
				fill.scale = tm_scale_categorical(), 
				fill.chart = tm_chart_bar())
```

```{r}
tm_shape(World) + 
	tm_polygons("economy", 
				fill.scale = tm_scale_categorical(), 
				fill.chart = tm_chart_donut())
```

```{r}
tm_shape(World) +
	tm_polygons(tm_vars(c("HPI", "well_being"), multivariate = TRUE),
				fill.chart = tm_chart_heatmap())
```
