create_raster_plot: create_raster_plot

View source: R/create_raster_plot.R

create_raster_plotR Documentation

create_raster_plot

Description

The function is wrapper around ggplot2::geom_raster() that plots from a data.frame with columns for x and y coordinates along with an associated numeric attribute.

The function accepts a data frame with columns for numeric x/y values and an attribute that will be mapped as a color or fill aesthetic. Function provides additional ggplot2 text labeling and axis scaling.

Usage

create_raster_plot(
  df,
  aes_x = NULL,
  aes_y = NULL,
  aes_color = NULL,
  aes_fill = NULL,
  interpolate = FALSE,
  title = NULL,
  subtitle = NULL,
  caption = NULL,
  center_titles = FALSE,
  x_title = NULL,
  y_title = NULL,
  hide_x_tics = FALSE,
  hide_y_tics = FALSE,
  panel_color = "white",
  panel_border_color = "black",
  x_limits = NULL,
  x_major_breaks = waiver(),
  x_minor_breaks = waiver(),
  x_labels = waiver(),
  y_limits = NULL,
  y_major_breaks = waiver(),
  y_minor_breaks = waiver(),
  y_labels = waiver(),
  scale_breaks = waiver(),
  scale_values = NULL,
  scale_limits = NULL,
  scale_labels = waiver(),
  scale_colors = heat.colors(8),
  scale_na_value = "gray50",
  own_scale = FALSE,
  show_legend = TRUE,
  legend_pos = "right",
  legend_key_width = 0.5,
  legend_key_height = 0.7,
  legend_key_backgrd = "white"
)

Arguments

df

The required data.frame derived from a raster file (such as *.tiff) to be plotted.

aes_x

The required name of aesthetic variable from 'df' for the x dimension.

aes_y

The required name of aesthetic variable from 'df' for the y dimension.

aes_color

The variable name from 'df' for the attribute dependent aesthetic mapping for color.

aes_fill

The variable name from 'df' for the attribute dependent aesthetic mapping for fill.

interpolate

A logical which if TRUE interpolate linearly.

title

A string that sets the plot title.

subtitle

A string that sets the plot subtitle.

caption

A string that sets the plot caption

center_titles

A logical which if TRUE centers both the 'title' and 'subtitle'.

x_title

A string that sets the x axis title. If NULL (the default) then the x axis title does not appear.

y_title

A string that sets the y axis title. If NULL (the default) then the y axis title does not appear.

hide_x_tics

A logical that controls the appearance of the x axis tics.

hide_y_tics

A logical that controls the appearance of the y axis tics.

panel_color

A string in hexidecimal or color name that sets the plot panel's color. The default is "white".

panel_border_color

A string in hexidecimal or color name that sets the plot panel's border color. The default is "black".

x_limits

Depending on the class of 'aes_x', a numeric/Date/POSIXct 2 element vector that sets the minimum and maximum for the x axis. Use NA to refer to the existing minimum and maximum.

x_major_breaks

Depending on the class of 'aes_x', a numeric/Date/POSIXct vector or function that defines the exact major tic locations along the x axis.

x_minor_breaks

Depending on the class of 'aes_x', a numeric/Date/POSIXct vector or function that defines the exact minor tic locations along the x axis.

x_labels

A character vector with the same length as 'x_major_breaks', that labels the major tics.

y_limits

A numeric 2 element vector that sets the minimum and maximum for the y axis. Use NA to refer to the existing minimum and maximum.

y_major_breaks

A numeric vector or function that defines the exact major tic locations along the y axis.

y_minor_breaks

A numeric vector or function that defines the exact minor tic locations along the y axis.

y_labels

A character vector with the same length as 'y_major_breaks', that labels the major tics.

scale_breaks

A string/numeric vector that defines the scale breaks.

scale_values

A string/numeric vector that defines the possible values.

scale_limits

A string/numeric vector that defines the scale limits.

scale_labels

An optional string vector that defines the scale labels. Vector must be the same length as scale_breaks.

scale_colors

Vector of colors to use for n-color gradient.

scale_na_value

A string that sets the color for missing values.

own_scale

A logical which if TRUE, then your own scaling may be appended to the plot without using the above scale_* parameters.

show_legend

A logical that controls the appearance of the legend.

legend_pos

A string that sets the legend position. Acceptable values are "top", "bottom", "left", "right".

legend_key_width

A numeric that sets the legend width in cm.

legend_key_height

A numeric that sets the legend height in cm.

legend_key_backgrd

A string that sets the legend's background color.

Value

A ggplot class object.

Examples

library(ggplot2)
library(RplotterPkg)

RplotterPkg::create_raster_plot(
  df = RplotterPkg::kentucky_elevation,
  title = "County Elevations in Southeast Kentucky",
  aes_x = "x",
  aes_y = "y",
  aes_fill = "elevation"
) +
ggplot2::geom_sf(
  data = RplotterPkg::kentucky_counties,
  aes(x = NULL, y = NULL),
  alpha = 0,
  linewidth = 1.5
) +
ggplot2::geom_sf_text(
  data = RplotterPkg::kentucky_counties,
  aes(x = NULL, y = NULL, label = ID)
)


deandevl/RplotterPkg documentation built on March 1, 2025, 11:17 a.m.