Title: | Visualizing Social Science Data with 'ggplot2' |
Version: | 0.0.7 |
Description: | A 'ggplot2' extension for implementing parliament charts and several other useful visualizations. |
URL: | https://github.com/erocoar/ggpol |
License: | MIT + file LICENSE |
Encoding: | UTF-8 |
LazyData: | true |
Depends: | ggplot2 (≥ 3.3.0), R (≥ 3.4.0) |
Imports: | grid, gtable, grDevices, plyr, rlang, dplyr, tibble, glue |
RoxygenNote: | 7.1.0 |
Suggests: | knitr, rmarkdown |
Collate: | 'facet_share.R' 'utilities.R' 'geom_arcbar.R' 'geom_bartext.R' 'geom_boxjitter.R' 'geom_circle.R' 'geom_confmat.R' 'geom_parliament.R' 'geom_tshighlight.R' 'ggproto-classes.R' 'stat-arcbar.R' 'stat-boxjitter.R' 'stat-circle.R' 'stat-confmat.R' 'stat-parliament.R' |
NeedsCompilation: | no |
Packaged: | 2020-11-08 13:24:59 UTC; fjtie |
Author: | Frederik Tiedemann [aut, cre] |
Maintainer: | Frederik Tiedemann <fj.tiedemann@googlemail.com> |
Repository: | CRAN |
Date/Publication: | 2020-11-08 13:40:02 UTC |
ggplot extensions
Description
ggplot extensions
Confusion Matrix
Description
Plot a confusion matrix.
Usage
geom_confmat(
mapping = NULL,
data = NULL,
stat = "confmat",
position = "identity",
width = NULL,
height = NULL,
annotate = TRUE,
text.perc = FALSE,
text.digits = 3,
text.alpha = NULL,
text.angle = NULL,
text.colour = NULL,
text.family = NULL,
text.fontface = NULL,
text.group = NULL,
text.hjust = NULL,
text.lineheight = NULL,
text.size = NULL,
text.vjust = NULL,
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
stat_confmat(
mapping = NULL,
data = NULL,
geom = "tile",
position = "identity",
show.legend = NA,
inherit.aes = TRUE,
na.rm = FALSE,
normalize = FALSE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
width |
The tile height. |
height |
The tile width. |
annotate |
Boolean indicator for whether to include values as text in each tile. |
text.perc |
Boolean indicator for whether to add '%' to the end of each value. |
text.digits |
Numeric vector equal to length one, indicating the number of digits to display. 'round(x, text.digits)'. |
text.alpha |
Alpha value for the text grobs. |
text.angle |
Angle value for the text grobs. |
text.colour |
Colour of the text grobs. |
text.family |
Font family of the text grobs. |
text.fontface |
Font face of the text grobs. |
text.group |
Groups of the text grobs. |
text.hjust |
Hjust of the text grobs. |
text.lineheight |
Lineheight of the text grobs. |
text.size |
Size of the text grobs. |
text.vjust |
Vjust of the text grobs. |
... |
Other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom |
The geometric object to use display the data |
normalize |
Boolean indicator for whether to scale the frequency values. |
Examples
x <- sample(LETTERS[seq(4)], 50, replace = TRUE)
y <- sample(LETTERS[seq(4)], 50, replace = TRUE)
ggplot() +
geom_confmat(aes(x = x, y = y), normalize = TRUE, text.perc = TRUE)
A shared axis for two panels
Description
'facet_share' uses [facet_wrap()] to build two panels with a shared axis.
Usage
facet_share(
facets,
scales = "fixed",
reverse_num = FALSE,
shrink = TRUE,
labeller = "label_value",
as.table = TRUE,
switch = NULL,
drop = TRUE,
dir = "h",
strip.position = "top"
)
Arguments
facets |
A set of variables or expressions quoted by For compatibility with the classic interface, can also be a
formula or character vector. Use either a one sided formula, |
scales |
Should scales be fixed ( |
reverse_num |
Used when passing on flipped data (times -1) for the second (right/bottom) panel. If 'TRUE', this will multiply the axis labels for that panel by -1. |
shrink |
If |
labeller |
A function that takes one data frame of labels and
returns a list or data frame of character vectors. Each input
column corresponds to one factor. Thus there will be more than
one with |
as.table |
If |
switch |
By default, the labels are displayed on the top and
right of the plot. If |
drop |
If |
dir |
Direction: either |
strip.position |
By default, the labels are displayed on the top of
the plot. Using |
Create an Arc-Barchart
Description
An arc bar diagram that allows for spacing between the individual arc components and spans 180 degrees.
Usage
geom_arcbar(
mapping = NULL,
data = NULL,
stat = "arcbar",
position = "identity",
n = 360,
sep = 0.05,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
stat_arcbar(
mapping = NULL,
data = NULL,
geom = "arcbar",
position = "identity",
n = 360,
sep = 0.05,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
n |
The number of |
sep |
Separation between the different shares, as a total proportion of pi. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
geom |
The geometric object to use display the data |
Aesthetics
geom_arcbar understands the following aesthetics (required aesthetics are in bold):
- **shares** - **r0** - inner radius - **r1** - outer radius - color - fill - linetype - alpha
Examples
bt <- data.frame(
parties = factor(c("CDU", "CSU", "AfD", "FDP", "SPD",
"Linke", "Gruene", "Fraktionslos"),
levels = c("CDU", "CSU", "AfD", "FDP", "SPD",
"Linke", "Gruene", "Fraktionslos")),
seats = c(200, 46, 92, 80, 153, 69, 67, 2),
colors = c("black", "blue", "lightblue", "yellow", "red",
"purple", "green", "grey"),
stringsAsFactors = FALSE)
ggplot(bt) +
geom_arcbar(aes(shares = seats, r0 = 5, r1 = 10, fill = parties)) +
scale_fill_manual(values = bt$colors) +
coord_fixed() +
theme_void()
Repelling text for GeomBar.
Description
Repelling text for GeomBar.
Usage
geom_bartext(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
spacing = 0.003,
dir = "v",
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
parse |
If |
nudge_x |
Horizontal and vertical adjustment to nudge labels by. Useful for offsetting text from points, particularly on discrete scales. |
nudge_y |
Horizontal and vertical adjustment to nudge labels by. Useful for offsetting text from points, particularly on discrete scales. |
spacing |
Defaults to 0.003. Minimum spacing between labels in NPC units. |
dir |
Defaults to "v", i.e. vertical repel of overlapping groups of labels. Can alternatively be set to "h" for horizontal repel. |
check_overlap |
If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
Examples
df <- data.frame(L = rep(LETTERS[1:2], each = 4),
l = rep(letters[1:4], 2),
val = c(96.5, 1, 2, 0.5, 48, 0.7, 0.3, 51))
ggplot(df, aes(x = L, y = val, fill = l)) +
geom_bar(stat = "identity") +
geom_bartext(aes(label = paste0(val, "%")), position = position_stack(vjust = 0.5)) +
ggtitle("GeomBartext")
A hybrid boxplot.
Description
Half boxplot, half scatterplot with customizable jitter.
Usage
geom_boxjitter(
mapping = NULL,
data = NULL,
stat = "BoxJitter",
position = "dodge",
...,
outlier.colour = NULL,
outlier.color = NULL,
outlier.fill = NULL,
outlier.shape = 19,
outlier.size = 1.5,
outlier.stroke = 0.5,
outlier.alpha = NULL,
outlier.intersect = FALSE,
jitter.colour = NULL,
jitter.color = NULL,
jitter.fill = NULL,
jitter.shape = 19,
jitter.size = 1.5,
jitter.stroke = 0.5,
jitter.alpha = NULL,
jitter.position = ggplot2::PositionJitter,
jitter.params = list(width = NULL, height = NULL),
boxplot.expand = FALSE,
notch = FALSE,
notchwidth = 0.5,
varwidth = FALSE,
errorbar.draw = FALSE,
errorbar.length = 0.5,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
Use to override the default connection between
|
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
outlier.colour |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.color |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.fill |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.shape |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.size |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.stroke |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.alpha |
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
outlier.intersect |
Defaults to 'FALSE'. If set to 'TRUE', outliers will be part of the jitter-plot (but keeping the given outlier graphical parameters) rather than plotted vertically above / below the whisker lines. |
jitter.colour , jitter.color , jitter.fill , jitter.shape , jitter.size , jitter.stroke , jitter.alpha |
Default aesthetics for jitter, set to 'NULL' to inherit from the aesthetics used for the box. |
jitter.position |
Position object used for calculating jitter (defaults to 'ggplot2::PositionJitter'). |
jitter.params |
Parameters passed to 'jitter.position' (for 'ggplot2::PositionJitter', this is 'width', 'height' and 'seed'). |
boxplot.expand |
Defaults to 'FALSE'. If set to 'TRUE', the full boxplots will be plotted. |
notch |
If |
notchwidth |
For a notched box plot, width of the notch relative to
the body (defaults to |
varwidth |
If |
errorbar.draw |
Draw horizontal whiskers at the top and bottom (the IQR). Defaults to 'FALSE'. |
errorbar.length |
Length of the horizontal whiskers (errorbar). Defaults to half the width of the half-boxplot, or half the width of the entire boxplot if 'boxplot.expand' is set to 'TRUE'. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Examples
set.seed(221)
df <- data.frame(score = rgamma(150, 4, 1),
gender = sample(c("M", "F"), 150, replace = TRUE),
genotype = factor(sample(1:3, 150, replace = TRUE)))
ggplot(df) + geom_boxjitter(aes(x = gender, y = score, fill = genotype),
jitter.shape = 21, jitter.color = NA,
outlier.color = NA, errorbar.draw = TRUE) +
scale_fill_manual(values = c("#CF3721", "#31A9B8", "#258039")) +
theme_minimal()
Circles with pre-defined radii.
Description
Similar to '[geom_point()]', but allows for full control of the size.
Usage
geom_circle(
mapping = NULL,
data = NULL,
stat = "circle",
position = "identity",
n = 360,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
stat_circle(
mapping = NULL,
data = NULL,
geom = "circle",
position = "identity",
n = 360,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
n |
The number of points calculated for the circle polygon, defaults to 360. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
geom |
The geometric object to use display the data |
Aesthetics
geom_circle understands the following aesthetics (required aesthetics are in bold):
- **x** - x-coordinate of center - **y** - y-coordinate of center - **r** - radius - color - fill - linetype - alpha
Examples
set.seed(22189)
df <- data.frame(x = sample(1:10, 3), y = sample(1:10, 3),
r = sample(3:4, 3, replace = TRUE))
ggplot(df) + geom_circle(aes(x = x, y = y, r = r, fill = gl(3, 1))) +
coord_fixed()
Create a Parliament Diagram
Description
Draws a parliament diagram based on parties' member counts, where each point in the arc represents a single member of parliament. Parties are plotted right-to-left.
Usage
geom_parliament(
mapping = NULL,
data = NULL,
stat = "parliament",
position = "identity",
r0 = 1.5,
r1 = 3,
n = 360,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
stat_parliament(
mapping = NULL,
data = NULL,
geom = "parliament",
position = "identity",
r0 = 1.5,
r1 = 3,
n = 360,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
r0 |
Inner radius, defaults to 1.5. |
r1 |
Outer radius, defaults to 3. |
n |
Number of passed to 'StatCircle', defaults to 360. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
geom |
The geometric object to use display the data |
Aesthetics
geom_parliament understands the following aesthetics (required aesthetics are in bold): - **seats** - number of seats of the parties - fill - color - linetype - alpha
Computed variables
- x,y
coordinates of individual MPs
Examples
bt <- data.frame(
parties = factor(c("CDU", "CSU", "AfD", "FDP", "SPD",
"Linke", "Gruene", "Fraktionslos"),
levels = c("CDU", "CSU", "AfD", "FDP", "SPD",
"Linke", "Gruene", "Fraktionslos")),
seats = c(200, 46, 92, 80, 153, 69, 67, 2),
colors = c("black", "blue", "lightblue", "yellow",
"red","purple", "green", "grey"),
stringsAsFactors = FALSE)
ggplot(bt) +
geom_parliament(aes(seats = seats, fill = parties), color = "black") +
scale_fill_manual(values = bt$colors, labels = bt$parties) +
coord_fixed() +
theme_void()
Timeseries highlighting
Description
This is a version of [ggplot2::geom_rect()] that defaults to spanning the entirety of the y-axis.
Usage
geom_tshighlight(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Aesthetics
geom_tshighlight understands the following aesthetics (required aesthetics are in bold): - **xmin** - **xmax** - fill - color
Examples
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line() +
geom_tshighlight(aes(xmin = as.Date("01/01/1990", format = "%d/%m/%Y"),
xmax = as.Date("01/01/2000", format = "%d/%m/%Y")),
alpha = 0.01)