8 Charts

There are various ways to create charts, static or interactive, in R.

8.0.1 Static charts with ggplot2

Static charts can be created with ggplot2 (in PNG format). By default they have a width=7 and height=5. You can alter this in the code chunk options: {r fig.width=8, fig.height=4}
For full reference see: http://ggplot2.tidyverse.org/reference/index.html

If you only want to chart frequencies, then you only need to specify the x axis variable. In the geom layer add stat=“count” to automatically generate a count.f

8.0.3 Chart Themes & Styles

You can change the appearance of most elements of the chart use a ‘theme’ layer. There are various templates available such as theme_classic (see https://ggplot2.tidyverse.org/reference/ggtheme.html):

You can create a customised theme to : * Change the appeareance of axes (axis.line)
* Change the plot area (panel.background)
* Add/Remove/Change the gridlines (panel.grid.major.x & panel.grid.major.y)
* Change the position and size of the title (plot.title)

You can create reusable styles and themes, which can be applied to all charts:

8.0.5 Bar Charts (geom_bar & geom_histogram)

geom_bar produces basic bar charts and is best used with categorical variables.
geom_histogram allows you to choose bin widths to automatically re-group you data, so it is better for continuous variables.

To flip a bar chart from vertical to horizontal use ‘coord_flip’

8.0.9 Combination Charts

You can have multiple layers, for example bars and points

8.0.10 Scales

scales package

8.0.11 ggplot wizard

The esquisse package has a shiny wizard that can generate code for different chart types:

8.0.12 Interactive charts with ggiraph

The ggiraph package extends ggplot and allows interactivity.

## Warning: package 'ggiraph' was built under R version 3.4.4

8.0.13 Interactive charts with dygraph

For interactive time series charts you can use the dygraph package which is related to the dygraph javascript library.

There are lots of options that can be changed such as axis labels, legends, and colours.