Description Usage Arguments Details Examples
View source: R/annotation_shading_polygon.R
ggplot2::annotation_raster can only 
draw shading rectangles. However, this 
function can draw polygons of any shape 
with shading colors. See the shape 
argument and the raster argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | annotation_shading_polygon(
  shape = data.frame(c(-1, 1, 0), c(0, 0, 1.732)),
  xmin = NULL,
  xmax = NULL,
  ymin = NULL,
  ymax = NULL,
  raster = NULL,
  interpolate = TRUE,
  result_interpolate = TRUE,
  shape_trim = NULL,
  raster_trim = NULL,
  result_trim = NULL,
  result = c("layer", "magick"),
  width = 800,
  height = NULL,
  res = 72
)
 | 
shape | 
 the polygon can be 
a data frame (or matrix object, or tbl_df object) 
with x and y coordinates (that is, with two columns), 
a plot created by ggplot or an image 
read into R by   | 
xmin | 
 the left side of the position to 
put the polygon. When 
  | 
xmax | 
 the right side.  | 
ymin | 
 the bottom side.  | 
ymax | 
 the top side.  | 
raster | 
 the shading colors. 
It can be a raster object, 
a matrix of colors, a ggplot plot or an 
image read into R by 
  | 
interpolate | 
 the   | 
result_interpolate | 
 whether to interpolate in the 
final result which is essentially an output of 
  | 
shape_trim | 
 this argument 
decides whether to trim edges 
of   | 
raster_trim | 
 whether to trim raster. 
Most of the time we do want to trim the raster.
However, the   | 
result_trim | 
 how to trim the final result. If you find your figure loses some parts, you can try to turn this off. Default is NULL.  | 
result | 
 when it is "layer", the function is a ggplot layer. When it is "magick", the function only create an image.  | 
width | 
 the width which will be passed 
to   | 
height | 
 the height which will be passed 
to   | 
res | 
 resolution in pixels which will be passed to 
  | 
height can be used in the 
following ways: 
 (1) an integer which will be 
directly passed to image::graph.
 (2) a character-like integer, 
e.g., height = "0.5". Suppose width = 400, 
the height that will be used is 400*0.5 = 200. 
This effectively prevents the image from becoming 
too large.
 (3) height = "coord_fixed". 
the ratio between height and width will 
be (top-bottom)/(right-left). And top, bottom,  
right and left are extreme values of shape 
when the latter is of class data.frame/matrix/gg.
 (4) height = "image". the width and height 
will be the width and height of raster when raster is 
a magick object.
 (5) height = NULL, the default. 
Now height is computed automatically. 
A ratio is computed first,  
ratio = (top-bottom)/(right-left). if the ratio is larger 
than 5 or smaller than 0.2, then height will be 
width*5 or width*0.2; else, the height will be treated 
in the same way as in (3) above. If shape is 
of class gg and it has uses coord_flip(), the 
height will be automatically adjusted. All these works 
are needed to prevent the image from becoming too large.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  | # Example 1
poly=ellipsexy(-1, 0, a=1, b=1)
m=matrix(rainbow(7))
ggplot()+
	coord_fixed()+
	annotation_shading_polygon(
		poly, raster=m
	)+
	annotation_shading_polygon(
		poly, raster=m, 
		xmin=1, xmax=5, 
		ymin=-1, ymax=1, 
	)
#
# Example 2, only an image
tt=annotation_shading_polygon(
	poly, result="magick", 
	width=280, height=280
)
#
# Example 3, both shape and raster are 
# ggplot plots.
p1=ggplot()+geom_tile(aes(x=1: 5, y=1: 5))
p2=ggplot()+geom_polygon(aes(x=c(0, 1, 1, 0), 
		y=c(0, 0, 1, 1)), fill="red")+theme_void()
ggplot()+coord_fixed()+
	annotation_shading_polygon(
		shape=p1, 
		xmin=1, xmax=10, 
		ymin=1, ymax=5, 
		raster=p2
	)
 | 
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.