Is there some way to use a specific small image as a point in a scatterplot with ggplot2. Ideally I will want to resize the images based on an variable.
Here's an example:
library(ggplot2) p <- ggplot(mtcars, aes(wt, mpg)) p + geom_point(aes(size = qsec, shape = factor(cyl)))
So I basically want to know if there is a way to supply a specific image as the shape?
To add a picture to a plot in base R, we first need to read the picture in the appropriate format and then rasterImage function can be used. The most commonly used format for picture in R is PNG. A picture in PNG format can be added to a plot by supplying the values in the plot where we want to add the picture.
%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.
The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.
ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics]. Some documentation can be found on the ggplot website .
Here's a minimalist geom to display raster images instead of points,
library(ggplot2) library(grid) ## replace by a named list with matrices to be displayed ## by rasterGrob .flaglist <- list("ar" = matrix(c("blue", "white", "blue"), 1), "fr" = matrix(c("blue", "white", "red"), 1)) flagGrob <- function(x, y, country, size=1, alpha=1){ grob(x=x, y=y, country=country, size=size, cl = "flag") } drawDetails.flag <- function(x, recording=FALSE){ for(ii in seq_along(x$country)){ grid.raster(x$x[ii], x$y[ii], width = x$size[ii]*unit(1,"mm"), height = x$size[ii]*unit(0.5,"mm"), image = .flaglist[[x$country[[ii]]]], interpolate=FALSE) } } scale_country <- function(..., guide = "legend") { sc <- discrete_scale("country", "identity", scales::identity_pal(), ..., guide = guide) sc$super <- ScaleDiscreteIdentity class(sc) <- class(ScaleDiscreteIdentity) sc } GeomFlag <- ggproto("GeomFlag", Geom, required_aes = c("x", "y", "country"), default_aes = aes(size = 5, country="fr"), draw_key = function (data, params, size) { flagGrob(0.5,0.5, country=data$country, size=data$size) }, draw_group = function(data, panel_scales, coord) { coords <- coord$transform(data, panel_scales) flagGrob(coords$x, coords$y, coords$country, coords$size) } ) geom_flag <- function(mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...) { layer( geom = GeomFlag, mapping = mapping, data = data, stat = stat, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list(na.rm = na.rm, ...) ) } set.seed(1234) d <- data.frame(x=rnorm(10), y=rnorm(10), country=sample(c("ar","fr"), 10, TRUE), stringsAsFactors = FALSE) ggplot(d, aes(x=x, y=y, country=country, size=x)) + geom_flag() + scale_country()
(output from the ggflags package)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With