如何将图像用作图例键字形? [英] How to use an image as a legend key glyph?

查看:74
本文介绍了如何将图像用作图例键字形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用图标在ggplot中绘制一个散点图,并且坚持使用图标来生成图例.

THere是ggimage文档中的一个名为"draw_key_image"的函数,但我真的不了解如何使用它或所需的参数.

文档说数据=包含要在此键中显示的缩放比例外观的单行数据框",但这对我完全没有帮助!

(v0.3.0)创建于2020-04-20 sup>

有用的线程:将svg转换为png

I am plotting a scatterplot in ggplot with icons and I am stuck with producing a legend using the icons.

THere is a function in the ggimage documentation called "draw_key_image" but I don't really understand how to use it or the required arguments.

The documentation says "data = A single row data frame containing the scaled aesthetics to display in this key" but this does not help me at all!

https://www.rdocumentation.org/packages/ggimage/versions/0.2.7/topics/draw_key

##Hopefully reproducible code with example data

##Icons downloaded from https://labs.mapbox.com/maki-icons/
library(ggplot2)
library(ggimage)
library(rsvg)

Activity<-"Walk"
x = -2
y = 2.5
icon<-".\\mapbox-maki-a6d16d4\\icons\\shoe-15.svg"
test_data<-data.frame( Activity, x, y, icon)

p_test<-ggplot(data = test_data,
          aes(x = x, 
              y = y))+
  geom_image(aes(image=icon), size=.03)
  p_test

Please could someone help me add a legend with the icon and label of "Walk"?

Many thanks, Jo

解决方案

This draw_key function is something that is supposed to work 'under the hood'. See documentation

Each geom has an associated function that draws the key when the geom needs to be displayed in a legend. These functions are called draw_key_*(), where * stands for the name of the respective key glyph. The key glyphs can be customized for individual geoms by providing a geom with the key_glyph argument (see layer() or examples below.)

I dare say, ggimage may possibly deserve some more development in this regard.

https://github.com/GuangchuangYu/ggimage/issues/18 shows that currently only three types of legend glyphs are supported, and you activate them with changing the options (see code below). One can also change the underlying draw_key function and instead of loading the R image, you can load your image. It needs to be a png though, so first you need to convert it to png.

You can find the source code which I modified here

The disadvantage is that it currently only accepts one image. In order to map several images to an aesthetic of your geom, you could find inspiration in grandmaster Baptiste's creation of a 'minimalist' geom. https://stackoverflow.com/a/36172385/7941188

library(ggplot2)
library(ggimage)

Activity<-c("Walk1", "Walk2")
x = 1:2
y = 2:3

icon<-"https://raw.githubusercontent.com/mapbox/maki/a6d16d49a967b73d9379890a7b26712b12b8daef/icons/shoe-15.svg"
bitmap <- rsvg::rsvg(icon, height = 50)

png::writePNG(bitmap, "shoe.png", dpi = 144)

test_data<-data.frame( Activity, x, y, icon)

options(ggimage.keytype = "image")

ggplot(data = test_data, aes(x = x, y = y)) +
  geom_image(aes(image=icon, color = Activity), size=.03)


The icon changed to the R logo:

Now let's modify the draw_key_image function. You also need to call it with the key_glyph argument in geom_image.


draw_key_image <- function (data, params, size) {
  kt <- getOption("ggimage.keytype")
if (kt == "image") {
    img <- magick::image_read("shoe.png")
    grobs <- lapply(seq_along(data$colour), function(i) {
      img <- ggimage:::color_image(img, data$colour[i], data$alpha[i])
      grid::rasterGrob(0.5, 0.5, image = img, width = 1, height = 1)
    })
    class(grobs) <- "gList"
    keyGrob <- ggplot2:::ggname("image_key", grid::gTree(children = grobs))
  }
  return(keyGrob)
}

ggplot(data = test_data, aes(x = x, y = y)) +
  geom_image(aes(image=icon, color = Activity), size=.03, 
             key_glyph = draw_key_image) # you need to add this argument

Created on 2020-04-20 by the reprex package (v0.3.0)

helpful threads: convert svg to png

这篇关于如何将图像用作图例键字形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆