Skip to content

Commit 7c61bd5

Browse files
committed
v.0.0.8 add piechart
1 parent 9825448 commit 7c61bd5

6 files changed

Lines changed: 153 additions & 1 deletion

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: SuperPlotR
22
Title: Making SuperPlots in R
3-
Version: 0.0.7
3+
Version: 0.0.8
44
Authors@R:
55
person(given = "Stephen J",
66
family = "Royle",

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export(get_sp_colours)
77
export(get_sp_shapes)
88
export(get_sp_stats)
99
export(get_sp_summary)
10+
export(pieplot)
1011
export(representative)
1112
export(superplot)
1213
import(cowplot)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# SuperPlotR (development version)
22

3+
## SuperPlotR 0.0.8
4+
5+
* Added `pieplot()` to conveniently make pie charts with ggplot2
6+
37
## SuperPlotR 0.0.7
48

59
* Added ability to find representative datapoints with `representative()`

R/pieplot.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#' Make a pie chart using ggplot2
2+
#'
3+
#' @description
4+
#' The function \code{pie_maker} creates a pie chart using
5+
#' \code{ggplot2}. It takes two vectors of values and optionally a
6+
#' second vector of values to create a pie chart with two layers.
7+
#' @param x1 A numeric vector containing the values for the first
8+
#' layer of the pie chart.
9+
#' @param cols A character vector containing the colors for the
10+
#' slices of the pie chart.
11+
#' @param x2 An optional numeric vector containing the values for the
12+
#' second layer of the pie chart.
13+
#' @param label An optional character string to be used as the title
14+
#' @param ... Currently not used.
15+
#' @examples
16+
#' pieplot(x1 = c(143, 459),
17+
#' cols = c("#44aa99", "#117733"))
18+
#'
19+
#' pieplot(x1 = c(61 - 9, 9, 593, 3595 - 593),
20+
#' cols = c("#bbbbbb", "#44aa99", "#117733", "#dddddd"),
21+
#' x2 = c(61, 3595))
22+
#'
23+
#' @return ggplot object
24+
#' @import ggplot2
25+
#'
26+
#' @export
27+
28+
29+
pieplot <- function(x1, cols, x2 = NULL, label = NULL, ...) {
30+
pcolours <- ymin <- ymax <- NULL
31+
df <- data.frame(
32+
pvalues = x1,
33+
pcolours = as.factor(seq_along(cols))
34+
)
35+
cs <- cumsum(x1)
36+
df$ymin <- c(0, cs[seq_along(cs) - 1])
37+
df$ymax <- cs
38+
if (is.null(x2)) {
39+
df2 <- df
40+
} else {
41+
cs <- cumsum(x2)
42+
df2 <- data.frame(
43+
pvalues = x2,
44+
ymin = c(0, cs[seq_along(cs) - 1]),
45+
ymax = cs
46+
)
47+
}
48+
49+
p <- ggplot() +
50+
geom_rect(data = df, aes(fill = pcolours,
51+
ymin = ymin, ymax = ymax, xmin = 0, xmax = 3)) +
52+
scale_fill_manual(values = cols) +
53+
geom_rect(data = df2, aes(ymin = ymin, ymax = ymax,
54+
xmin = 0, xmax = 3),
55+
alpha = 0, colour = "black") +
56+
xlim(c(0, 3)) +
57+
theme_minimal() +
58+
theme(aspect.ratio = 1) +
59+
coord_polar(theta = "y") +
60+
theme(
61+
axis.text = element_blank(),
62+
axis.ticks = element_blank(),
63+
axis.title = element_blank(),
64+
panel.grid = element_blank(),
65+
legend.position = "none"
66+
)
67+
if (!is.null(label)) {
68+
p <- p + labs(title = label)
69+
}
70+
71+
return(p)
72+
}

man/pieplot.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/pieplot.Rmd

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: "Making non-SuperPlots - PiePlots"
3+
output: rmarkdown::html_vignette
4+
vignette: >
5+
%\VignetteIndexEntry{pieplot}
6+
%\VignetteEngine{knitr::rmarkdown}
7+
%\VignetteEncoding{UTF-8}
8+
---
9+
10+
```{r, include = FALSE}
11+
knitr::opts_chunk$set(
12+
collapse = TRUE,
13+
comment = "#>"
14+
)
15+
```
16+
17+
```{r setup}
18+
library(SuperPlotR)
19+
```
20+
21+
# Making non-SuperPlots - FlatPlots
22+
23+
For convenience, SuperPlotR also provides a function to make pie charts. Called
24+
`pieplot()`, it takes a vector of values and a vector of colours, and returns a
25+
pie chart using `ggplot2`. It can also take a second vector of values to create
26+
a pie chart with two layers.
27+
28+
```{r pieplot1}
29+
pieplot(x1 = c(143, 459),
30+
cols = c("#44aa99", "#117733"))
31+
```
32+
```{r pieplot2}
33+
pieplot(x1 = c(61 - 9, 9, 593, 3595 - 593),
34+
cols = c("#bbbbbb", "#44aa99", "#117733", "#dddddd"),
35+
x2 = c(61, 3595))
36+
```

0 commit comments

Comments
 (0)