https://github.com/xuan-liang/ggmatplot
An R package for plotting wide-format data
https://github.com/xuan-liang/ggmatplot
data-visualization r-package
Last synced: 3 months ago
JSON representation
An R package for plotting wide-format data
- Host: GitHub
- URL: https://github.com/xuan-liang/ggmatplot
- Owner: xuan-liang
- License: gpl-3.0
- Created: 2019-07-03T11:25:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-16T23:40:50.000Z (about 1 year ago)
- Last Synced: 2025-10-22T03:48:19.286Z (3 months ago)
- Topics: data-visualization, r-package
- Language: HTML
- Homepage: https://xuan-liang.github.io/ggmatplot/
- Size: 32.8 MB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "80%",
message = FALSE,
warning = FALSE
)
```
[](https://zenodo.org/badge/latestdoi/195039918)
# ggmatplot
`ggmatplot` is a quick and easy way of plotting the columns of two matrices or
data frames against each other using [`ggplot2`](https://ggplot2.tidyverse.org/).
## Overview
[`ggplot2`](https://ggplot2.tidyverse.org/) requires wide format data to be
wrangled into long format for plotting, which can be quite cumbersome when
creating simple plots. Therefore, the motivation for `ggmatplot` is to provide
a solution that allows [`ggplot2`](https://ggplot2.tidyverse.org/) to handle
wide format data. Although `ggmatplot` doesn't provide the same flexibility
as [`ggplot2`](https://ggplot2.tidyverse.org/), it can be used as a
workaround for having to wrangle wide format data into long format and creating
simple plots using [`ggplot2`](https://ggplot2.tidyverse.org/).
`ggmatplot` is built upon [`ggplot2`](https://ggplot2.tidyverse.org/), and its
functionality is inspired by
[`matplot`](https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/matplot).
Therefore, `ggmatplot` can be considered as a `ggplot` version of
[`matplot`](https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/matplot).
Similar to [`matplot`](https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/matplot),
`ggmatplot` plots a vector against the columns of a matrix, or the columns of
two matrices against each other, or a vector/matrix on its own. However, unlike
[`matplot`](https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/matplot),
`ggmatplot` returns a `ggplot` object. Therefore,
[ggplot add ons](https://ggplot2.tidyverse.org/reference/index.html) such as
scales, faceting specifications, coordinate systems and themes can also be added
on to the resultant `ggplot` object.
## Installation
The latest version can be installed from [CRAN](https://CRAN.R-project.org/package=ggmatplot):
```{r eval=FALSE}
install.packages("ggmatplot")
```
Or you can install the development version from [GitHub](https://github.com/xuan-liang/ggmatplot):
```{r eval=FALSE}
# install.packages("devtools")
devtools::install_github("xuan-liang/ggmatplot")
```
## Examples
```{r}
library(ggmatplot)
```
The first example plots a vector against each column of matrix using the default `plot_type = "point"` of `ggmatplot()`. We consider a simple case that we have a covariate vector `x` and a matrix `z` with the response `y` and the fitted value `fit.y` as the two columns.
```{r point}
# vector x
x <- c(rnorm(100, sd = 2))
# matrix z
y <- x * 0.5 + rnorm(100, sd = 1)
fit.y <- fitted(lm(y ~ x))
z <- cbind(
actual = y,
fitted = fit.y
)
ggmatplot(x, z)
```
If two matrices with equal number of columns are used, the corresponding columns of the matrices will be plotted against each other, i.e. column 1 of matrix `y` will be plotted against column 1 of matrix `x`, column 2 of matrix `y` will be plotted against column 2 of matrix `x`, etc.
The next example uses the iris dataset, with matrices `x` and `y` as shown below. The `Sepal.Width` is plotted against `Sepal.Length` and the `Petal.Width` is plotted against `Petal.Length`. Therefore the groups 'Column 1' and 'Column 2' can be interpreted as 'Sepal' and 'Petal' respectively. To make the plot more meaningful, we can further modify the legend label and axis names by `legend_label`, `xlab` and `ylab`.
```{r point-columns}
x <- (iris[, c(1, 3)])
head(x, 5)
y <- (iris[, c(2, 4)])
head(y, 5)
ggmatplot(x, y)
ggmatplot(x, y,
xlab = "Length",
ylab = "Width",
legend_label = c("Sepal", "Petal")
)
```
The next example creates a line plot of vector `x` against the columns of matrix `y` by using `plot_type = "line"`. Although the lines would be represented using different colors by default, the `color` parameter allows custom colors to be assigned to them.
The following plot assigns custom colors to the lines, and the limits of the y axis are updated using the `ylim` parameter. Further, a ggplot theme is added on to the resultant ggplot object.
```{r line}
# matrix x
x <- 1:10
# matrix y
y <- cbind(
square = x^2,
cube = x^3
)
ggmatplot(x, y,
plot_type = "line",
color = c("blue", "purple"),
ylim = c(0, 750)
) +
theme_minimal()
```
Next is the plot of the US personal expenditure over 5 categories and 5 years, and is a simple example of how wide format data can be used with `ggmatplot()`. Note how the expenditure categories to be used on the x axis is used as vector `x`, and the expenditure values is used in wide format as matrix `y` - with its columns corresponding to the grouping structure.
The plot specifies the plot type as `plot_type = "both"`, which is a combination of 'point' and 'line' plots. It is further customized by using `ggmatplot()` parameters and a `ggplot` theme as well.
```{r both}
USPersonalExpenditure
# vector x
x <- rownames(USPersonalExpenditure)
ggmatplot(x, USPersonalExpenditure,
plot_type = "both",
xlab = "Category",
ylab = "Expenditure (in Billions of Dollars)",
legend_title = "Year",
legend_label = c(1940, 1945, 1950, 1955, 1960)
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
Density plots only accept a single matrix or data frame and will group the plot based on its columns. The following density plot uses a two column matrix, and groups the plot by the two columns. While the default density estimate is represented in the measurement units of the data, an aesthetic mapping is added on to the ggplot object to scale the density estimate to a maximum of 1.
```{r density}
# matrix x
x <- (iris[, 1:2])
ggmatplot(x, plot_type = "density") +
aes(y = stat(scaled)) +
theme_bw()
```
Boxplots accept only a single matrix or data frame as well, and uses its columns as individual groups. By default, the fill color is white. But it is easy to customize and the transparency can be modified by the `alpha` parameter.
It is also worth noticing that `alpha` isn't a parameter defined in `ggmatplot()`, but can be used. This is because `ggmatplot` is built upon `ggplot2`, and each `plot_type` corresponds to a [`geom`](https://ggplot2.tidyverse.org/reference/index.html#section-geoms) as listed [here](https://xuan-liang.github.io/ggmatplot/reference/ggmatplot.html#plot-types). Therefore, all valid parameters with the underlying [`ggplot2 geom`](https://ggplot2.tidyverse.org/reference/index.html#section-geoms) can be used with `ggmatplot()`.
```{r boxplot}
# matrix x
x <- (iris[, 1:4])
ggmatplot(x,
plot_type = "boxplot",
color = 'black',
fill = 'grey',
alpha = 0.8,
xlab = "", ylab = ""
)
```
Violin plots accept a single matrix or data frame input, and behave similar to density plots and boxplots.
This plot updates the colors of the two groups using the `color` parameter, and it can be seen that the fill of the violin plots has been updated too. This is because updating either the `color` or `fill` parameter will automatically update the other, unless they are both defined simultaneously.
```{r violin}
# matrix x
x <- (iris[, 1:2])
ggmatplot(x,
plot_type = "violin",
color = c("#00AFBB", "#E7B800"),
xlab = "", ylab = ""
)
```
Dotplots too accept a single matrix input and plot the distribution of each of its columns. The next example uses the `plot_type = "dotplot"` to visualize the distribution of the data with the custom color and binwidth value. Note that the default setting for binwidth is 1/30 of the range of the data.
```{r dotplot}
# matrix x
x <- (iris[, 1:2])
ggmatplot(x,
plot_type = "dotplot",
color = c("#00AFBB", "#E7B800"),
binwidth = 0.1,
xlab = "", ylab = ""
)
```
Similar to density, violin, dotplots, and box plots, histograms too accept a single matrix or data frame input and group the plot using its columns. The histogram in the following example uses a matrix of 4 columns, and therefore groups the plots based on these 4 columns. The plot is also faceted by group and the legend is removed by a `ggplot` theme setting.
The `color` and `fill` parameters have been defined simultaneously on this plot. However, only a single `color` value is defined whereas the number of `fill` colors correspond to the number of groups. If a single value is defined it will be used over all groups, like the black line color is used across all groups in this example.
```{r histogram}
# matrix x
x <- (iris[, 1:4])
ggmatplot(x,
plot_type = "histogram",
xlab = "",
color = "black",
fill = c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF")
) +
facet_wrap(~Group, scales = "free") +
theme(legend.position = 'none')
```
The next example is of the `plot_type = ecdf`, and also uses a single matrix input to plot out the empirical cumulative distributions of the columns of the matrix individually.
```{r ecdf}
# matrix x
x <- (iris[, 1:4])
ggmatplot(x,
plot_type = "ecdf",
xlab = "",
ylab = 'Empirical CDF',
size = 1
) +
theme_minimal()
```
Error plots also accept only a single matrix input, and plots out error bars for each column of the matrix. The `desc_stat` parameter of `ggmatplot()` can be used to define what the mid point and error bars of the plot should represent.
The next example, plots out an `errorplot` using the medians and interquartile ranges of each variable.
```{r errorplot}
# matrix x
x <- (iris[, 1:4])
ggmatplot(x,
plot_type = "errorplot",
desc_stat = "median_iqr",
xlab = "",
size = 1
) +
theme_minimal()
```