插入图像/PNG ggplot2-Cowplot [英] Insert Image/PNG ggplot2 - Cowplot

查看:62
本文介绍了插入图像/PNG ggplot2-Cowplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cowplot包的draw_image()函数.作为示例,我设法在图中获得了图像.

I'm trying to use cowplot package's draw_image() function. I've managed to get an image in a graph as an example.

我无法确定xy位置的工作方式,我不得不不断输入随机数,直到看到图像为止.

I can't work out how the xy locations work, I had to keep inputting random numbers until I got to see the image.

require(ggplot2) #required packages
require(cowplot)
require(magick)

p1 <- qplot(Sepal.Length, Sepal.Width, data = iris)

ggdraw(p1) +      
  draw_image(
    "https://upload.wikimedia.org/wikipedia/en/7/77/EricCartman.png",
    y = 0.2,
    scale = 0.5
  )

有人能建议他们发挥什么作用吗?它肯定看起来与图形的比例不同.

Can anyone advise on what scale they function? It sure doesn't seem to be the same scale as the graph.

推荐答案

它位于绘图的坐标中.可能使您感到困惑的是,ggdraw()正在建立一个新的坐标系,它在x和y中都从0到1运行.如果要在绘图坐标中绘制图像,则无需使用ggdraw().只需将draw_plot()直接添加到图中即可.

It's in the coordinates of the plot. The point that may be confusing you is that ggdraw() is setting up a new coordinate system running from 0 to 1 in both x and y. If you want to draw an image in the plot coordinates, there's no need to use ggdraw(). Just add draw_plot() directly to the plot.

library(ggplot2)
library(cowplot)
theme_set(theme_bw())

p1 <- qplot(Sepal.Length, Sepal.Width, data = iris)

# ggdraw() sets up a new coordinate system running from 0 to 1. This
# allows you to place an image on top of the plot.
ggdraw(p1) + 
  draw_image("https://upload.wikimedia.org/wikipedia/en/7/77/EricCartman.png")

# if you want to draw the image into the plot, don't use ggdraw()
p1 + draw_image(
  "https://upload.wikimedia.org/wikipedia/en/7/77/EricCartman.png",
  x = 5, y = 2.5, width = 2, height = 1.5
)

reprex软件包(v0.2.1)于2018年12月19日创建 sup>

Created on 2018-12-19 by the reprex package (v0.2.1)

这篇关于插入图像/PNG ggplot2-Cowplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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