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

# The 1st argument in ggplot() should be the name of your dataframe;
# The 2nd argument should be an aesthetic attribute - this defines which variables will be on the x and y axes.
# You can now add 'Layers' to the ggplot object using the + symbol. You must have at least one 'geom' layer that specifies what type of chart it is (eg. geom_bar, geom_point, geom_line etc..) 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.2 Changing Axes
By default the axes labels are based on variable names. To change these or add a title use a ‘labs’ layer:
ggplot2::ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(stat="identity", colour="red") +
labs(title = "Cars", x = 'Horsepower', y = "Miles/Gallon") 
The width of the x and y axes are set automatically, but can be changed:
ggplot2::ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(colour="red") +
labs(title = "Cars", x = 'Horsepower', y = "Miles/Gallon") +
expand_limits(y = c(0,40), x = c(0,350)) + # Customise where the x and y axes start/end
scale_y_continuous(expand=c(0,0)) + # Removes The gap at the bottom of the y axis
scale_x_continuous(expand=c(0,0)) # Removes The gap at left of the x axis
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):
ggplot2::ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(stat="identity", colour="red") +
labs(title = "Car Efficiency", x = 'Horsepower', y = "Miles/Gallon") +
expand_limits(y = 0) +
scale_y_continuous(expand=c(0,0)) +
scale_x_continuous(expand=c(0,0)) +
theme_classic()
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)
ggplot2::ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(stat="identity", colour="red") +
theme(axis.line = element_line(colour = "black", size = 0.25, linetype = "solid")
,axis.ticks = element_blank()
,panel.background = element_blank()
,panel.grid.major.y = element_line(colour = "blue", size = 0.1, linetype = "dashed")
,panel.grid.major.x = element_line(colour = "green", size = 0.1, linetype = "dashed")
,plot.title = element_text(hjust = 0.5) ) You can create reusable styles and themes, which can be applied to all charts:
custom_axis <- list(expand_limits(y = 0, x = 0),
scale_y_continuous(expand=c(0,0)),
scale_x_continuous(expand=c(0,0)) )
custom_theme <- theme(axis.line = element_line(colour = "grey", size = 0.25, linetype = "solid")
,axis.ticks = element_blank()
,panel.background = element_blank()
,panel.grid.major.y = element_line(colour = "grey", size = 0.1, linetype = "dashed")
,panel.grid.major.x = element_line(colour = "grey", size = 0.1, linetype = "dashed")
,plot.title = element_text(hjust = 0.5) )
ggplot2::ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(stat="identity", colour="red") +
custom_axis +
custom_theme 
8.0.4 Line Charts (geom_line)

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.
ggplot2::ggplot(mtcars, aes(x = hp)) +
geom_bar(stat="count", fill="orange", width=1) # Use **colour=** to set the border colour

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

8.0.6 Adding features
Extra layers can be added to the chart to show additional information.
Vertical or horizontal lines can be used to show statistics.
ggplot2::ggplot(mtcars, aes(x = hp)) +
geom_histogram(binwidth = 25, alpha=0.5, fill="orange") +
geom_vline(aes(xintercept = median(hp)),col='red',size=1) +
geom_vline(aes(xintercept = quantile(hp, 0.25)),col='red',size=0.5) +
geom_vline(aes(xintercept = quantile(hp, 0.75)),col='red',size=0.5)
8.0.7 Heatmaps
esoph %>% group_by(agegp, alcgp) %>% summarise(n = sum(ncontrols)) %>%
ggplot(aes(x = agegp, y = alcgp, fill = n)) +
geom_tile()
8.0.8 Boxplots

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.
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths) #Creates the data set for the graph
dygraphs::dygraph(lungDeaths) # Displays the chart with default settings#It is possible to add a range selector under the chart
dygraphs::dygraph(lungDeaths) %>% dygraphs::dyRangeSelector()There are lots of options that can be changed such as axis labels, legends, and colours.