Anscombe's quartet comprises four datasets that have nearly identical simple descriptive statistics, yet appear very different when graphed. (See Wikipedia link below)
11 observations (x, y) per group

https://en.wikipedia.org/wiki/Anscombe%27s_quartet
Four groups
11 observations (x, y) per group

https://en.wikipedia.org/wiki/Anscombe%27s_quartet
Base graphics
Trellis plots
lattice packageggplot2 package
ggplot2 is a widely used R package that extends R's visualization capabilities. It takes the hassle out of things like creating legends, mapping other variables to scales like color, or faceting plots
Where does the "gg" in ggplot2 come from? The ggplot2 package provides an R implementation of Leland Wilkinson's Grammar of Graphics (1999)
https://ggplot2.tidyverse.org/
Specifically, ggplot2 allows you to build a plot layer-by-layer by specifying:
aesthetics that map variables in the data to axes on the plot or to plotting size, shape, color, etc.,
a geom, which specifies how the data are represented on the plot (points, lines, bars, etc.),
a stat, a statistical transformation or summary of the data applied prior to plotting,
facets, which we've already seen above, that allow the data to be divided into chunks on the basis of other categorical or continuous variables and the same plot drawn for each chunk.
| Object | Description | |
|---|---|---|
| Data | The raw data that you want to plot | |
| Aethetics | aes() | How to map your data on x, y axis, color, size, shape (aesthetics) |
| Geometries | geom_ | The geometric shapes that will represent the data |
data + aesthetic mappings of data to plot coordinates + geometry to represent the dataSpecify data, aesthetics and geometric shapes
ggplot(data, aes(x=, y=, color=, shape=, size=, fill=)) +geom_point(), or geom_histogram(), or geom_boxplot(), etc.
This combination is very effective for exploratory graphs.
The data must be a data frame in a long (not wide) format
The aes() function maps columns of the data frame to aesthetic properties of geometric shapes to be plotted.
ggplot() defines the plot; the geoms show the data; layers are added with +
diamonds %>% filter(cut == "Good", color == "E") %>% ggplot(aes(x = price, y = carat)) + geom_point() # aes(size = price) +
Try other geoms
geom_smooth() # method = lm geom_line() geom_boxplot() geom_bar(stat="identity") geom_histogram()ggplot + geomsCustomizing scales
Scales control the mapping from data to aesthetics and provide tools to read the plot (ie, axes and legends).
Every aesthetic has a default scale. To add or modify a scale, use a scale function.
All scale functions have a common naming scheme:
scale _ name of aesthetic _ name of scale
Examples: scale_y_continuous, scale_color_discrete, scale_fill_manual
ggplot(iris, aes(x = Petal.Width, y = Sepal.Width, color=Species)) + geom_point() + scale_y_continuous(limits=c(0,5), breaks=seq(0,5,0.5))

ggplot(iris, aes(x = Petal.Width, y = Sepal.Width, color=Species)) + geom_point() + scale_color_manual(name="Iris Species", values=c("red","blue","black"))

Sometimes, one needs to create separate plots of subsets of data. These are called facets in ggplot2. Think par(mfrow=c(2,2)) analogy
Use facet_wrap() if you want to facet by one variable and have ggplot2 control the layout. Think 1D ribbon wrapped into 2D. Example:
facet_wrap( ~ var)facet_grid() if you want to facet by one and/or two variables and control layout yourself. Think 2D grid. Examples: facet_grid(. ~ var1) - facets in columns facet_grid(var1 ~ .) - facets in rows facet_grid(var1 ~ var2) - facets in rows and columns facet_wrapNote free x scales
ggplot(iris, aes(x = Petal.Width, y = Sepal.Width)) + geom_point() + geom_smooth(method="lm") + facet_wrap(~ Species, scales = "free_x")

gridExtra R package for more custom plot arrangementlibrary(gridExtra)p1 <- iris %>% filter(Species == "setosa") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()p2 <- iris %>% filter(Species == "versicolor") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()grid.arrange(p1, p2, ncol = 2)

patchwork for simple plot arrangementlibrary(patchwork)p1 <- iris %>% filter(Species == "setosa") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()p2 <- iris %>% filter(Species == "versicolor") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()p3 <- iris %>% filter(Species == "virginica") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()p1 + p2 + p3

https://patchwork.data-imaginist.com/
patchwork for simple plot arrangementlibrary(patchwork)p1 <- iris %>% filter(Species == "setosa") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()p2 <- iris %>% filter(Species == "versicolor") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()p3 <- iris %>% filter(Species == "virginica") %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_smooth()(p1 | p2) / p3

https://patchwork.data-imaginist.com/
All geoms perform a default statistical transformation.
For example, geom_histogram() bins the data before plotting. geom_smooth() fits a line through the data according to a specified method.
In some cases the transformation is the "identity", which just means plot the raw data. For example, geom_point()
These transformations are done by stat functions. The naming scheme is stat_ followed by the name of the transformation. For example, stat_bin, stat_smooth, stat_boxplot
Every geom has a default stat, every stat has a default geom.
stat="identity"# ToothGrowth describes the effect of Vitamin C on Tooth growth in Guinea pigsdf <- data.frame(dose = c("D0.5", "D1", "D2"), len = c(4.2, 10, 29.5))ggplot(data=df, aes(x=dose, y=len)) + geom_bar(stat="identity")

# Horizontal bar plotggplot(data=df, aes(x=dose, y=len)) + geom_bar(stat="identity") + coord_flip()

ggplot2 barplots : Quick start guide - R software and data visualization
The default ggplot2 theme is excellent. It follows the advice of several landmark papers regarding statistics and visual perception. (Wickham 2009, p. 141)
However you can change the theme using ggplot2's themeing system. To date, there are seven built-in themes: theme_gray (default), theme_bw, theme_linedraw, theme_light, theme_dark, theme_minimal, theme_classic
Explore the cowplot R package by Claus Wilke, and its themes theme_cowplot(), theme_half_open(), theme_minimal_grid(), etc.
You can also update axis labels and titles using the labs function
https://wilkelab.org/cowplot/index.html
ggplot(iris, aes(x = Petal.Width, y = Sepal.Width, color=Species)) + geom_point() + labs(title="Sepal vs. Petal", x="Petal Width (cm)", y="Sepal Width (cm)")

ggplot(iris, aes(x = Petal.Width, y = Sepal.Width, shape=Species)) + geom_point() + theme_bw()

cowplot - publication-quality plotslibrary(cowplot)ggplot(iris, aes(x = Petal.Width, y = Sepal.Width, shape=Species, color = Species)) + geom_point() + theme_cowplot()

https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
cowplot - publication-quality plotslibrary(cowplot)ggplot(iris, aes(x = Petal.Width, y = Sepal.Width, shape=Species, color = Species)) + geom_point() + theme_minimal_grid()

https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
cowplot - publication-quality plotslibrary(cowplot)p1 <- ggplot(mtcars, aes(disp, mpg)) + geom_point()p2 <- ggplot(mtcars, aes(qsec, mpg)) + geom_point()plot_grid(p1, p2, labels = c('A', 'B'), label_size = 12)

data(mpg)ggplot(mpg, aes(x = class)) + geom_bar()

https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
class_agg <- data.frame(table(mpg$class))names(class_agg) <- c("class", "count")ggplot(class_agg, aes(x = class, y = count)) + geom_bar(aes(fill = class), stat = "identity")

class_agg <- data.frame(table(mpg$class))names(class_agg) <- c("class", "count")ggplot(class_agg, aes(x = count, y = class)) + geom_bar(aes(fill = class), stat = "identity")

Map data directly to the proper axes. Previously, coord_flip() was used.
library(forcats)class_agg <- data.frame(table(mpg$class))names(class_agg) <- c("class", "count")ggplot(class_agg, aes(x = count, y = fct_reorder(class, count))) + geom_bar(aes(fill = class), stat = "identity")

https://forcats.tidyverse.org/reference/index.html
ggplot(mpg, aes(x = hwy)) + geom_density()

ggplot(mpg, aes(x = hwy)) + geom_histogram() + geom_density(aes(y=2 * ..count..))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mpg, aes(x = hwy, fill = class)) + geom_histogram(position = "stack")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mpg, aes(x = hwy, fill = class)) + geom_histogram(position = "dodge")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# useful for assessing percentagesggplot(mpg, aes(x = hwy, fill = class)) + geom_histogram(position = "fill")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.## Warning: Removed 35 rows containing missing values (geom_bar).
ggplot(iris, aes(x = Sepal.Width, y = Petal.Width)) + geom_smooth() + geom_smooth(method = "lm", color = "seagreen", se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'## `geom_smooth()` using formula 'y ~ x'
ggplot(iris, aes(x = Sepal.Width, y = Petal.Width)) + geom_point(aes(color = Species))

| Parameter | Description | |
|---|---|---|
| Facets | facet_ | Split one plot into multiple plots based on a grouping variable |
| Scales | scale_ | Maps between the data ranges and the dimensions of the plot |
| Visual Themes | theme | The overall visual defaults of a plot: background, grids, axe, default typeface, sizes, colors, etc. |
| Statistical transformations | stat_ | Statistical summaries of the data that can be plotted, such as quantiles, fitted curves (loess, linear models, etc.), sums etc. |
| Coordinate systems | coord_ | Expressing coordinates in a system other than Cartesian |
diamonds %>% # Start with the 'diamonds' dataset filter(cut == "Ideal") %>% # Then, filter rows where cut == Ideal ggplot(aes(price)) + # Then, plot using ggplot geom_histogram() + # and plot histograms facet_wrap(~ color) + # in a 'small multiple' plot, broken out by 'color' ggtitle("Diamond price distribution per color") + labs(x="Price", y="Count") + theme(panel.background = element_rect(fill="lightblue")) + theme(plot.title = element_text(family="Trebuchet MS", size=28, face="bold", hjust=0, color="#777777")) + theme(axis.title.y = element_text(angle=0)) + theme(panel.grid.minor = element_blank())pdf() (or any other graphical device, e.g., jpeg, png, svg) and dev.off() works
ggsave() saves the ggplot object (or, the latest plot) into a file. File extension defines the graphical device
p <- ggplot(mpg, aes(x = class)) + geom_bar()ggsave(filename = "test.jpg", plot = p, width = 7, height = 10, units = c("in"), dpi = 300)suppressMessages(library(plotly))p <- ggplot(iris, aes(x = Sepal.Width, y = Petal.Width)) + geom_point(aes(color = Species))pp <- ggplotly(p)pp
# Save with # htmlwidgets::saveWidget(pp, "test_plotly.html")
# BiocManager::install("talgalili/heatmaply")suppressMessages(library(heatmaply))suppressMessages(library(RColorBrewer))heatmaply(scale(mtcars), colors = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100))
https://github.com/talgalili/heatmaply
p1 <- ggplot(mtcars, aes(disp, mpg)) + geom_point()p2 <- ggplot(mtcars, aes(qsec, mpg)) + geom_point()x <- list(p1, p2)lapply(x, print)


## [[1]]
## ## [[2]]
# for (i in 1:length(x)) {# print(x[i])# }Inkscape - vector graphics editor. Works with Scalable Vector Graphics (SVG) format. Export in any format, at any resolution.
svg graphic device in R. ggsave() also saves graphs in svg formatGIMP - raster graphics editor. Think Photoshop.
Anscombe's quartet comprises four datasets that have nearly identical simple descriptive statistics, yet appear very different when graphed. (See Wikipedia link below)
11 observations (x, y) per group

https://en.wikipedia.org/wiki/Anscombe%27s_quartet
Keyboard shortcuts
| ↑, ←, Pg Up, k | Go to previous slide |
| ↓, →, Pg Dn, Space, j | Go to next slide |
| Home | Go to first slide |
| End | Go to last slide |
| Number + Return | Go to specific slide |
| b / m / f | Toggle blackout / mirrored / fullscreen mode |
| c | Clone slideshow |
| p | Toggle presenter mode |
| t | Restart the presentation timer |
| ?, h | Toggle this help |
| Esc | Back to slideshow |