Title: | Data Visualization for IP Addresses and Networks |
---|---|
Description: | A 'ggplot2' extension that enables visualization of IP (Internet Protocol) addresses and networks. The address space is mapped onto the Cartesian coordinate system using a space-filling curve. Offers full support for both IPv4 and IPv6 (Internet Protocol versions 4 and 6) address spaces. |
Authors: | David Hall [aut, cre] |
Maintainer: | David Hall <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.2.9000 |
Built: | 2024-12-07 04:09:38 UTC |
Source: | https://github.com/davidchall/ggip |
A ggplot2 coordinate system that maps a range of IP address space onto a two-dimensional grid using a space-filling curve.
coord_ip()
forms the foundation of any ggip plot. It translates all
ip_address
and ip_network
vectors to Cartesian coordinates, ready for use by ggplot2 layers (see
Accessing Coordinates). This ensures all layers use a common mapping.
coord_ip( canvas_network = ip_network("0.0.0.0/0"), pixel_prefix = 16, curve = c("hilbert", "morton"), expand = FALSE )
coord_ip( canvas_network = ip_network("0.0.0.0/0"), pixel_prefix = 16, curve = c("hilbert", "morton"), expand = FALSE )
canvas_network |
An |
pixel_prefix |
An integer scalar that sets the prefix length of the network represented by a single pixel. The default value is 16. Increasing this effectively improves the resolution of the plot. |
curve |
A string to choose the space-filling curve. Choices are
|
expand |
If |
coord_ip()
stores the result of the mapping in a nested data frame column.
This means each ip_address
or
ip_network
column in the original data set is
converted to a data frame column. When specifying ggplot2 aesthetics, you'll
need to use $
to access the nested data (see Examples).
Each ip_address
column will be replaced with a
data frame containing the following columns:
Column name | Data type | Description |
ip |
ip_address |
Original IP data |
x |
integer |
Pixel x |
y |
integer |
Pixel y |
Each ip_network
column will be replaced with a
data frame containing the following columns:
Column name | Data type | Description |
ip |
ip_network |
Original IP data |
xmin |
integer |
Bounding box xmin |
ymin |
integer |
Bounding box ymin |
xmax |
integer |
Bounding box xmax |
ymax |
integer |
Bounding box ymax |
vignette("visualizing-ip-data")
describes the mapping in more detail.
suppressPackageStartupMessages(library(dplyr)) tibble(address = ip_address(c("0.0.0.0", "128.0.0.0", "192.168.0.1"))) %>% ggplot(aes(x = address$x, y = address$y, label = address$ip)) + geom_point() + geom_label(nudge_x = c(10, 0, -10), nudge_y = -10) + coord_ip(expand = TRUE) + theme_ip_light() tibble(network = ip_network(c("0.0.0.0/8", "224.0.0.0/4"))) %>% mutate( start = network_address(network), end = broadcast_address(network) ) %>% ggplot() + geom_point(aes(x = start$x, y = start$y), color = "blue") + geom_point(aes(x = end$x, y = end$y), color = "red") + geom_rect( aes(xmin = network$xmin, xmax = network$xmax, ymin = network$ymin, ymax = network$ymax), alpha = 0.5, fill = "grey" ) + coord_ip(curve = "morton", expand = TRUE) + theme_ip_light()
suppressPackageStartupMessages(library(dplyr)) tibble(address = ip_address(c("0.0.0.0", "128.0.0.0", "192.168.0.1"))) %>% ggplot(aes(x = address$x, y = address$y, label = address$ip)) + geom_point() + geom_label(nudge_x = c(10, 0, -10), nudge_y = -10) + coord_ip(expand = TRUE) + theme_ip_light() tibble(network = ip_network(c("0.0.0.0/8", "224.0.0.0/4"))) %>% mutate( start = network_address(network), end = broadcast_address(network) ) %>% ggplot() + geom_point(aes(x = start$x, y = start$y), color = "blue") + geom_point(aes(x = end$x, y = end$y), color = "red") + geom_rect( aes(xmin = network$xmin, xmax = network$xmax, ymin = network$ymin, ymax = network$ymax), alpha = 0.5, fill = "grey" ) + coord_ip(curve = "morton", expand = TRUE) + theme_ip_light()
Computes and draws the outline of the Hilbert curve used to map IP data to the Cartesian plane. By superimposing this outline on top of a ggip plot, it guides the eye to regions that are close in IP address space.
geom_hilbert_outline( mapping = NULL, data = NULL, ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_hilbert_outline( mapping = NULL, data = NULL, ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
... |
Other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom_curve_outline()
understands the following aesthetics:
ip
: An ip_network
column. By default, the
entire Hilbert curve is shown.
curve_order
: How nested is the curve? (default: 3
).
closed
: Should the curve outline have closed ends? (default: FALSE
).
alpha
colour
linetype
linewidth
The start coordinates for the segment
The end coordinates for the segment
p <- ggplot() + coord_ip() + theme_ip_light() # default shows curve across entire canvas p + geom_hilbert_outline() # only show subnetwork p + geom_hilbert_outline(ip = ip_network("128.0.0.0/2")) # increased nesting p + geom_hilbert_outline(curve_order = 4) # show multiple networks df <- data.frame( ip = ip_network(c("0.0.0.0/2", "128.0.0.0/4")), curve_order = c(4, 5), closed = c(FALSE, TRUE) ) p + geom_hilbert_outline( aes(ip = ip, curve_order = curve_order, closed = closed), data = df )
p <- ggplot() + coord_ip() + theme_ip_light() # default shows curve across entire canvas p + geom_hilbert_outline() # only show subnetwork p + geom_hilbert_outline(ip = ip_network("128.0.0.0/2")) # increased nesting p + geom_hilbert_outline(curve_order = 4) # show multiple networks df <- data.frame( ip = ip_network(c("0.0.0.0/2", "128.0.0.0/4")), curve_order = c(4, 5), closed = c(FALSE, TRUE) ) p + geom_hilbert_outline( aes(ip = ip, curve_order = curve_order, closed = closed), data = df )
These functions are used internally by coord_ip()
to map
ip_address
and ip_network
vectors to Cartesian coordinates. They are exposed externally to support use
of these coordinates outside of ggplot2.
address_to_cartesian( address, canvas_network = ip_network("0.0.0.0/0"), pixel_prefix = 16, curve = c("hilbert", "morton") ) network_to_cartesian( network, canvas_network = ip_network("0.0.0.0/0"), pixel_prefix = 16, curve = c("hilbert", "morton") )
address_to_cartesian( address, canvas_network = ip_network("0.0.0.0/0"), pixel_prefix = 16, curve = c("hilbert", "morton") ) network_to_cartesian( network, canvas_network = ip_network("0.0.0.0/0"), pixel_prefix = 16, curve = c("hilbert", "morton") )
address |
An |
canvas_network |
An |
pixel_prefix |
An integer scalar that sets the prefix length of the network represented by a single pixel. The default value is 16. Increasing this effectively improves the resolution of the plot. |
curve |
A string to choose the space-filling curve. Choices are
|
network |
An |
A data.frame containing columns:
address_to_cartesian()
: x
and y
network_to_cartesian()
: xmin
, ymin
, xmax
and ymax
address_to_cartesian(ip_address("192.168.0.1")) network_to_cartesian(ip_network("224.0.0.0/4"))
address_to_cartesian(ip_address("192.168.0.1")) network_to_cartesian(ip_network("224.0.0.0/4"))
Addresses are grouped into networks determined by the pixel_prefix
argument
of coord_ip()
. Then the z
values are summarized with summary function fun
.
stat_summary_address( mapping = NULL, data = NULL, ..., fun = NULL, fun.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
stat_summary_address( mapping = NULL, data = NULL, ..., fun = NULL, fun.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
... |
Other arguments passed on to |
fun |
Summary function (see section below for details). If |
fun.args |
A list of extra arguments to pass to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
stat_summary_address()
understands the following aesthetics (required
aesthetics are in bold):
ip
: An ip_address
column
z
: Value passed to the summary function (required if fun
is used)
fill
: Default is after_stat(value)
alpha
The following variables are available to after_stat()
:
value
: Value of summary statistic
count
: Number of observations
The data
might contain multiple rows per pixel of the heatmap, so a summary
function reduces this information to a single value to display.
This function receives the data
column specified by the z
aesthetic
and also receives arguments specified by fun.args
.
The fun
argument can be specified in multiple ways:
NULL
If no summary function is provided, the number of observations
is computed. In this case, you don't need to specify the z
aesthetic,
and the computed variables value
and count
will be equal.
The name of an existing function (e.g. fun = "mean"
).
Either provide an existing function (e.g. fun = mean
) or
define a new function (e.g. fun = function(x) sum(x^2)
).
A function can also be created from a formula. This uses .x
as the summarized variable (e.g. fun = ~ sum(.x^2)
).
dat <- data.frame( ip = sample_ipv4(10000), weight = runif(10000) ) p <- ggplot(dat, aes(ip = ip)) + coord_ip() + theme_ip_light() # simple count of observations p + stat_summary_address() + scale_fill_viridis_c(trans = "log2", na.value = "black", guide = "none") # compute mean weight p + stat_summary_address(aes(z = weight), fun = ~ mean(.x)) + scale_fill_viridis_c(na.value = "black", guide = "none")
dat <- data.frame( ip = sample_ipv4(10000), weight = runif(10000) ) p <- ggplot(dat, aes(ip = ip)) + coord_ip() + theme_ip_light() # simple count of observations p + stat_summary_address() + scale_fill_viridis_c(trans = "log2", na.value = "black", guide = "none") # compute mean weight p + stat_summary_address(aes(z = weight), fun = ~ mean(.x)) + scale_fill_viridis_c(na.value = "black", guide = "none")
These set sensible defaults for plots generated by ggip.
Use ggplot2::theme()
if you want to tweak the results.
theme_ip_light(base_size = 11, base_family = "") theme_ip_dark( background_color = "black", text_color = "white", base_size = 11, base_family = "" )
theme_ip_light(base_size = 11, base_family = "") theme_ip_dark( background_color = "black", text_color = "white", base_size = 11, base_family = "" )
base_size |
base font size, given in pts. |
base_family |
base font family |
background_color |
Background color |
text_color |
Text color |
p <- ggplot(data.frame(ip = ip_address("128.0.0.0"))) + geom_point(aes(x = ip$x, y = ip$y), color = "grey") + coord_ip() p + theme_ip_light() p + theme_ip_dark()
p <- ggplot(data.frame(ip = ip_address("128.0.0.0"))) + geom_point(aes(x = ip$x, y = ip$y), color = "grey") + coord_ip() p + theme_ip_light() p + theme_ip_dark()