80 other geoms 2

Written by Ananya Jha and last updated on 5 February 2022

80.1 Introduction

Ggplot2 is powerful package that offers a wide variety of functions and options to produce graphics. So far, we have covered some important and useful individual geoms. In this lesson we will be talking about some other individual geoms that are used to create the basic layers of data.

In this lesson, you will learn how to:

Prerequisites:

  • Having the most updated version of the ggplot2 and maps package installed and loaded
  • Basic understanding of ggplot2 and experience working with the basic functions and aesthetics such as: fill, factor() etc.

Let’s first try to understand what these geoms are and take a quick look at their function definitions:

80.2 Density Plots

A Density plot(or Kernel density plot) is used to display the distribution of a numeric variable and can be created using the geom_density() function in R. It is similar to the histogram for continuous data but gives a smoothed version of the graph.

geom_density(mapping = NULL, data = NULL, stat = “density,” position = “identity,” …, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE, outline.type = “upper”)

Where:

Argument Details
mapping Set of aesthethic mappings created by aes(). By default, it is combined with the mapping specified in the top layer of the plot.
data The data to be dispayed. If not specified, inherited directly from top layer.
stat The statistical transformation to use on the data for this layer, as a string.
position The position adjustment. Is “identity” by default for density plots.
Other optional arguments(like aesthetics such as color/size etc.)
na.rm Can be used to remove missing values without a warning. Is FALSE by default.
orientation By default, orientation would follow from aesthetic mapping but to change manually you can set this to “x” or “y”
show.legend Set this to TRUE to include legend. Is NA by default which woulde include if aesthetics are mapped.

80.3 Violin Plots

Violin plots can be thought of as a hybrid of the box plot and kernel density plot. They are used with numerical data to display the distribution and the summary statistics. In R, violin plots can be created with the geom_violin() function.

geom_violin(mapping = NULL, data = NULL, stat = “ydensity,” position = “dodge,” …, draw_quantiles = NULL, trim = TRUE, scale = “area,” na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE)

Where:

Argument Details
mapping Set of aesthethic mappings created by aes(). By default, it is combined with the mapping specified in the top layer of the plot.
data The data to be displayed. If not specified, inherited directly from top layer.
stat The statistical transformation to use on the data for this layer, as a string.
position The position adjustment. Is “dodge” by default for violin plots
draw_quantiles Set to not(NULL) to draw lines to indicate quantiles
trim Set to FALSE to not trim the tails to match the range of data
scale By default all violins will have the same area. Set to “count” to scale to number of observations or to “width” to make all violins have same max width
Other optional arguments(like aesthetics such as color/size etc.)
na.rm Can be used to remove missing values without a warning. Is FALSE by default.
orientation By default, orientation would follow from aesthetic mapping but to change manually you can set this to “x” or “y”
show.legend Set this to TRUE to include legend. Is NA by default which woulde include if aesthetics are mapped.
inherit.aes Set this to FALSE to override default aesthetics inherited from the first layer

80.4 Polygons

To draw polygons, which can also be thought of as filled paths(more on this later), we use the geom_polygon() function. The function generally requires 2 data frames- one for the polygon coordinates(positions) and one for the values of each polygon(values). Polygons are considered very useful when working with map/spatial data or when you wish to layer simple polygons over other plots.

geom_polygon(mapping = NULL, data = NULL, stat = “identity,” position = “identity,” rule = “evenodd,” …, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Where:

Argument Details
mapping Set of aesthetic mappings created by aes(). By default, it is combined with the mapping specified in the top layer of the plot.
data The data to be displayed. If not specified, inherited directly from top layer.
stat The statistical transformation to use on the data for this layer, as a string.
position The position adjustment. Is “identity” by default for polygons.
Other optional arguments(like aesthetics such as color/size etc.)
na.rm Can be used to remove missing values without a warning. Is FALSE by default.
show.legend Set this to TRUE to include legend. Is NA by default which woulde include if aesthetics are mapped.
inherit.aes Set this to FALSE to override default aesthetics inherited from the first layer.

80.5 Rectangles

Rectangles are mainly used to draw surfaces on plots but sometimes we might want to draw random rectangles as well. As mentioned previously, there are 3 different functions in R that can be used to graph rectangles. We will be exploring the differences between these later.

geom_raster(mapping = NULL, data = NULL, stat = “identity,” position = “identity,” …, hjust = 0.5, vjust = 0.5, interpolate = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

geom_rect(mapping = NULL, data = NULL, stat = “identity,” position = “identity,” …, linejoin = “mitre,” na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

geom_tile(mapping = NULL, data = NULL, stat = “identity,” position = “identity,” …, linejoin = “mitre,” na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Where:

Function(s) Argument Details
geom_raster(), geom_rect(), geom_tile() mapping Set of aesthetic mappings created by aes(). By default, it is combined with the mapping specified in the top layer of the plot.
geom_raster(), geom_rect(), geom_tile() data The data to be displayed. If not specified, inherited directly from top layer.
geom_raster(), geom_rect(), geom_tile() stat The statistical transformation to use on the data for this layer, as a string.
geom_raster(), geom_rect(), geom_tile() position The position adjustment. Is “identity” by default for rectangles.
geom_raster(), geom_rect(), geom_tile() Other optional arguments(like aesthetics such as color/size etc.)
geom_raster() hjust, vjust Horizontal and Vertical justification for the plot(can be between 0-1). 0 would mean left-justified and 1 would be right-justified
geom_raster() interpolate Set this to TRUE to get a smoothed output
geom_raster(), geom_rect(), geom_tile() linejoin The line join style preferred
geom_raster(), geom_rect(), geom_tile() na.rm Can be used to remove missing values without a warning. Is FALSE by default.