5. ggplot2#

5.1. Getting started with ggplot2#

ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.

To use ggplot2 you first have to install the package.

5.1.1. Exercise 1#

Use the install.packages function to install ggplot2.

install.packages("ggplot2")
Hide code cell output
Installing package into ‘/home/grosed/R-notes-env/R-packages’
(as ‘lib’ is unspecified)

5.1.2. Example 1 - Simple line plot#

# load the package
library(ggplot2)

# generate some data
X<-seq(0,100,1)
Y<-cumsum(rnorm(101))
# ggplot likes data frames
df<-data.frame(X,Y)

# create a plot
p <- ggplot(df,aes(x=X,y=Y))

print(p)
_images/4557429088b23f2f465b6c9daece5709cf08d79574a6c3c7010f9438ac7a16a0.png

All this has done is create a plot with some data and specified an aesthetic using aes. In this case, the aesthetic associates the x axis with the X data and the y axis with the Y data. The plot exists and can be displayed. Notice that the plot is assigned to a variable. This is useful because the plot can now be modified through the variable (many plotting facilities do not support this) and thus allows you to program with plots.

Notice also that to display the plot you can use the print function.

Features are added to the plot using layers. A plot can have many layers.

5.1.3. Example 2 - Add some points#

# add a layer
p1 <- p + geom_point()
print(p1)
_images/4b7cef221cb92667c42a927f42deb6cc159f4c18af02de949d84d4b2675ff5a2.png

5.1.4. Example 3 - Add some lines#

p2 <- p + geom_line()
print(p2)
_images/22c273635e3ea372cd2ce3388e04f2926a8e55e986475d8769b9365a879cad85.png

5.1.5. Exercise 2#

Create a plot with both points and lines.

Hide code cell source
p3 <- p + geom_line() + geom_point()
print(p3)
Hide code cell output
_images/dd03d193b7ab25e93221810e596c78c11c44a6841b35b8a99afd2273c166a041.png

5.2. Customising appearance#

Each layer can be customised in a way appropriate for that layer.

5.2.1. Example 4 - Customise the lines#

p2 <- p + geom_line(color="red")
print(p2)
_images/de9cd9ab7fd7daad223bfbb4c7a4f58b6cfc71245f3df7757951bb676e0b6994.png

5.2.2. Exercise 3#

Experiment with the following code to change the appearance of the plot.

p2 <- p + geom_line(color="red",size=2,alpha=0.3,linetype=3)
print(p2)
Warning message:
“Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
 Please use `linewidth` instead.”
_images/088f4398841347fe555ceb7304cfa651334e2d70842c59de7b439cab5042109f.png

5.3. Themes#

The “style” of a plot can be modified and controlled through the use of themes.

5.3.1. Example 4 - A black and white theme.#

p2 <- p2 + theme_bw()
print(p2)
_images/64fd84b34d8973fdf7ab9b6177ca2cfb55815fa5a4ceb7d46bc9b1b8357e70fa.png

There are many themes to choose from. You can find out about them here

5.3.2. Exercise 4#

Experiment with some of the themes documented on the ggplot2 website

5.4. Labels#

It is easy to add labels to a plot

5.4.1. Example 5 - Adding Labels#

p2 <- p2 + labs(
    title = "Main Title",
    subtitle = "a subtitle",
    caption = "A caption",
    tag = "A tag",
    x = "x-axis",
    y = "y-axis"
  )
print(p2)
_images/93e04ec216fcc4e857997fafb3f6d74fdaa615358f50cda1f2b69179b4c9533f.png

5.5. More aesthetics#

It is possible to add aesthetics to a layer. You can think of an aesthetic as a mapping between the data and features of the layer. For example, you can make the colour change with the data.

5.5.1. Example 6 - Colour as a function of value#

p3<-p + geom_point(aes(color=Y))
print(p3)
_images/6f6af6c110408bfd5abb00d5c700b58f68ee85a2d698eca765dbf6f9edb52af6.png

5.5.2. Example 7 - Transparency as a function of value#

p3<-p + geom_point(aes(alpha=Y))
print(p3)
_images/e79d1351cac900db363762fbb5ba56b22670a58869d6641c7b66d79304a70424.png

5.5.3. Example 8 - Smoothing the data#

p3<-p + geom_point(aes(alpha=Y)) + geom_smooth()
print(p3)
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
_images/e953414e7a70322e4d4c5a09fce377bd14c9d04040290010a77acaa08a3283e3.png

5.6. Plot types#

There are lots of different plot types. A good way to find out about them is look through the some of the online galleries. A good starting point for a wide range of examples with code and data is the R graph library. I have picked a few and added them as examples.

# Data
a <- data.frame( x=rnorm(20000, 10, 1.9), y=rnorm(20000, 10, 1.2) )
b <- data.frame( x=rnorm(20000, 14.5, 1.9), y=rnorm(20000, 14.5, 1.9) )
c <- data.frame( x=rnorm(20000, 9.5, 1.9), y=rnorm(20000, 15.5, 1.9) )
df <- rbind(a,b,c)
 

5.6.1. Example 9 - Simple contour plot#

p <- ggplot(df, aes(x=x, y=y) )

p1 <- p + geom_density_2d()
print(p1)
_images/bee4412f534ad887d6ab2f8b2290ca41a199b69bf175e6615242b3d05ea82824.png
p2 <- p + stat_density_2d(aes(fill = ..level..), geom = "polygon")
print(p2)
Warning message:
“The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
 Please use `after_stat(level)` instead.”
_images/477d35e522ada5d08f536149d3944be252a1e558fe3e7677f048a7b991e49d40.png
p3 <- p + stat_density_2d(aes(fill = ..level..), geom = "polygon", colour="white")
print(p3)
_images/33761948dc8f8eac1299117c89678e39a34785b8509d2f622562a6c04a86cf16.png
p4 <- p + stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme(
    legend.position='none'
  )
print(p4)
_images/bdd88bc0d50886057cdad13ea1c4461d5d15650626330a2731f8c880125b464a.png

You can add extra data to a layer.

5.6.2. Example 10 - Adding data to a layer#

hotspots<-data.frame(x=c(10,14.5,9.5),y=c(10,14.5,15.5))
p4 <- p4 + geom_point(data=hotspots,aes(x=x,y=y),shape=23,fill="yellow",color="red")
plot(p4)
_images/f87240ed10e090df3c8aa0f6aff460194112b7385a96b9cf18272d44958e7656.png

5.7. Organising plots#

5.7.1. Exercise 5#

Install the gridExtra package.

Hide code cell source
install.packages("gridExtra")
Hide code cell output
Installing package into ‘/home/grosed/R-notes-env/R-packages’
(as ‘lib’ is unspecified)

5.7.2. Example 11 - Putting plots together in a grid#

library(gridExtra)
grid.arrange(p1,p2,p3,p4,nrow = 2,ncol=2)
_images/676c5350038ca19dd9c9204b83b18a69e847807ad80164e6294b43380a89034c.png

5.7.3. Exercise 6#

Find out mor details about gridExtra here and experiment with some different plot arrangements.

5.8. Saving plots#

5.8.1. Example 12 - Saving a plot to a pdf file.#

p5 <- grid.arrange(p1,p2,p3,p4,nrow = 2,ncol=2)
ggsave("mutiple-countor-plots.pdf",p5)
Saving 6.67 x 6.67 in image
_images/676c5350038ca19dd9c9204b83b18a69e847807ad80164e6294b43380a89034c.png

5.8.2. Exercise 13#

Use the help function to find out how to save a plot using a different file format.