1 Intended Learning Outcomes

1.1 By the end of today you will…

  • Understand how to create plots with ggplot package

  • Learn about how to use qplot() and ggplot() functions

  • Create your own plots and customise them

  • Have a basic understanding of very advanced HTML graphics

2 Introduction

  • ggplot2 is a system for decoratively creating graphics

  • It allows to create beautiful graphics and customize them

  • There are two main functions in ggplot package

    • qplot() for quick plots

    • ggplot() for more sophisticated graphics

  • Install and load ggplots library(ggplot2)

  • For more information read R Graphics Cookbook

3 Input Dataframe

I have created a dataframe which we will use for our plots

Lets make some graphs using this dataframe

4 Basic Scatter Graph

We can make basic graphs using the qplot

qplot(Height, Weight, data = Data.fr, geom = "point")

Using ggplot() the above is equivalent to

ggplot(data=Data.fr, aes(x=Height, y=Weight)) + geom_point()

5 Basic Line Graph

Instead of geom_point() in

ggplot(data=Data.fr, aes(x=Height, y=Weight)) + geom_point()

you can use geom_line(), geom_area(), geom_smooth(method = lm), or combination etc…

ggplot(data = Data.fr, aes(x = Height, y = Weight)) + geom_line() + geom_smooth(method = lm)

6 Basic Histogram

We shall mainly work with ggplot from now on. You can create histograms

ggplot(data = Data.fr, aes(Error1)) + geom_histogram(binwidth = 5)

Using qplot() the above is equivalent to

qplot(Error1,data=Data.fr, geom = "histogram", binwidth = 5)

7 Graphs With Colour

Customize your plots

ggplot(data = Data.fr, aes(x = Con, y = Weight)) + geom_bar(stat = "identity", 
    fill = "lightblue")

8 Boxplots

Create boxplot by factor

ggplot(data = Data.fr, aes(x = Dis, y = Weight, colour = Fac)) + geom_boxplot()

9 More Advanced Graphs 1

Create plots by factor

  ggplot(data = Data.fr) +
     geom_point(mapping = aes(x = Height, y = Weight, colour=Fac, shape=Con))

10 More Advanced Graphs 2

Create very complicated plots by factor

ggplot(data = Data.fr) + geom_point(mapping = aes(x = Height, y = Pressure, 
    colour = Fac, shape = Con, size = Day))

You can use alpha= instead of size

11 Caution with qgplot()

Note you are much more accurate using ggplot() for example see

P1 <- qplot(Height, Weight, data = Data.fr, shape = Con, colour = "red")
P2 <- ggplot(data = Data.fr) + geom_point(mapping = aes(x = Height, y = Weight, 
    shape = Con), color = "red")
grid.arrange(P1, P2, ncol = 2)

12 More Advanced Graphs and Smoothing line

Create plots by factor and add smooth lines

ggplot(data = Data.fr, mapping = aes(x = Height, y = Weight, colour = Fac)) + 
    geom_point() + stat_smooth(method = loess)

13 More Advanced Graphs Facets

Arrange facets

ggplot(data = Data.fr, mapping = aes(x = Height, y = Weight, colour = Fac)) + 
    geom_point() + facet_grid(Fac ~ Con) + stat_smooth(method = loess)

14 More Advanced Graphs

Different

ggplot(data = Data.fr, mapping = aes(x = Day, y = Weight, fill = Con)) + geom_area()

15 Advanced Graphs Heat Maps

Heat maps

library(scales)
ggplot(data = Data.fr) + geom_bar(aes(x = Day, y = Weight, fill = Con), stat = "identity", 
    position = "fill") + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    xlab("Day") + ylab("Weight") + scale_y_continuous(labels = percent_format()) + 
    guides(fill = guide_legend(title = NULL))

16 HTML Widgets library(plotly) 1

Interactive plots with library(plotly)

p <- ggplot(data = Data.fr, aes(x = Con, fill = Dis)) + geom_bar(position = "dodge")
ggplotly(p)

17 HTML Widgets library(plotly) 2

Interactive plots with library(plotly)

Data.fr$Day <- as.factor(Day)
p <- ggplot(data = Data.fr, aes(x = Day, y = Pressure, colour = Day)) + geom_boxplot()
ggplotly(p)

18 HTML Widgets library(plotly)

Interactive plots with library(plotly)

plot_ly(Data.fr, x = Data.fr$Height, y = Data.fr$Pressure, text = paste("Con: ", 
    Data.fr$Con), mode = "markers", color = Data.fr$Dis, size = Data.fr$Weight)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter

Read more on https://plot.ly/r/

19 HTML Widgets library(rbokeh)

Interactive plots library(rbokeh)

figure() %>% ly_points(Height, Weight, data = Data.fr, color = Con, glyph = Dis, 
    hover = list(Height, Weight))

20 HTML Widgets library(highcharter)

Interactive plots library(highcharter)

highchart() %>% hc_title(text = "Scatter chart with size and color") %>% hc_add_series_scatter(Data.fr$Height, 
    Data.fr$Pressure, Data.fr$Weight, Data.fr$Day)

21 HTML Widgets library(ggiraph)

Interactive plots library(ggiraph)

p <- ggplot(data = Data.fr, aes(x = Day, y = Pressure, colour = Day))
p <- p + geom_point_interactive(aes(tooltip = Dis), size = 2)
girafe(code = print(p))

22 HTML Widgets library(ggiraphExtra)

Interactive plots library(ggiraphExtra)

ggPoints(data = Data.fr, mapping = aes(x = Height, y = Weight, colour = Fac), 
    method = "lm", interactive = TRUE)