尝试将图像添加到极坐标图中会给出“错误:注释_自定义仅适用于笛卡尔坐标” [英] Trying to add an image to a polar plot gives "Error: annotation_custom only works with Cartesian coordinates"

查看:1313
本文介绍了尝试将图像添加到极坐标图中会给出“错误:注释_自定义仅适用于笛卡尔坐标”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着按照已经给出的答案将图片添加到图中,但在使用 coord_polar()

 #install时,它们不起作用.packages(RCurl,dependencies = TRUE)
library(RCurl)
myurl< - http://vignette2.wikia.nocookie.net/micronations/images/5/50/German_flag。 png

#install.packages(png,dependencies = TRUE)
library(png)
img< - readPNG(getURLContent(myurl))

#install.packages(ggplot2,dependencies = TRUE)
library(ggplot2)

ger < - grid :: rasterGrob(img,interpolate = TRUE)

##工作,将图像添加到图
ggplot(mtcars,aes(x = mpg,y = cyl))+ geom_line()+ annotation_custom(ger,10,15,5)

##不起作用
ggplot(mtcars,aes(x = mpg,y = cyl))+ geom_line()+ annotation_custom(ger)+ coord_polar()
>错误:annotation_custom仅适用于笛卡尔坐标

理想情况下,我希望能够定位标志图像在极坐标图的中心,沿着y轴的另一个图像。

是否有添加图像的方法?它可以是原样的,不需要转换。



我使用 ggplot2 版本2.0

cowplot 库让我在那里。



cowplot简介,您可以使用 draw_grob 函数根据需要叠加图像。它需要调整位置,因为位置会根据图的尺寸而变化,但可能会有所不同。



示例:

 #install.packages(RCurl,dependencies = TRUE)
library(RCurl)
myurl< - http://vignette2.wikia .nocookie.net / micronations / images / 5/50 / German_flag.png

#install.packages(png,dependencies = TRUE)
library(png)
img< - readPNG(getURLContent(myurl))

#install.packages(ggplot2,dependencies = TRUE)
library(ggplot2)

ger< ; - grid :: rasterGrob(img,interpolate = TRUE)
$ bg < - ggplot(mtcars,aes(x = mpg,y = cyl))+ geom_line()+ coord_polar()

#install.packages(cowplot,依赖关系= TRUE)
库(cowplot)
h < - ggdraw(g)
##调整以适合您的绘图
h + draw_grob(ger,0.4,0.48,0.07,0.07)


I've tried to follow the answer's given already for adding images to plots, but they do not work when using coord_polar()

# install.packages("RCurl", dependencies = TRUE)
library(RCurl)
myurl <- "http://vignette2.wikia.nocookie.net/micronations/images/5/50/German_flag.png"

# install.packages("png", dependencies = TRUE)
library(png)
img <-  readPNG(getURLContent(myurl))

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

ger <- grid::rasterGrob(img, interpolate=TRUE)

## works, adds the image to the plot
ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + annotation_custom(ger, 10, 15, 5)

## doesn't work
ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + annotation_custom(ger) + coord_polar()
> Error: annotation_custom only works with Cartesian coordinates

Ideally I'd like to be able to position the flag image within the center of the polar plot, and another image along the y-axis.

Is there anyway to add the image? It can be as-is, no transformation required.

I'm using ggplot2 version 2.0

解决方案

Gregor's suggestion for using the cowplot library has got me there.

Following the introduction to cowplot you can use the draw_grob function to overlay images as you like. It takes a bit of tweaking as the position changes depending on the dimensions of the plot, but its possible.

Example:

# install.packages("RCurl", dependencies = TRUE)
library(RCurl)
myurl <- "http://vignette2.wikia.nocookie.net/micronations/images/5/50/German_flag.png"

# install.packages("png", dependencies = TRUE)
library(png)
img <-  readPNG(getURLContent(myurl))

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

ger <- grid::rasterGrob(img, interpolate=TRUE)

g <- ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + coord_polar()

# install.packages("cowplot", dependencies = TRUE)
library(cowplot)
h <- ggdraw(g)
## tweak this to fit your plot
h + draw_grob(ger, 0.4, 0.48, 0.07, 0.07)

这篇关于尝试将图像添加到极坐标图中会给出“错误:注释_自定义仅适用于笛卡尔坐标”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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