简体   繁体   English

尝试将图像添加到极坐标图会给出“错误:annotation_custom 仅适用于笛卡尔坐标”

[英]Trying to add an image to a polar plot gives "Error: annotation_custom only works with Cartesian coordinates"

I've tried to follow the answer's given already for adding images to plots, but they do not work when using coord_polar()我已经尝试按照已经给出答案将图像添加到绘图中,但是在使用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.理想情况下,我希望能够将标志图像定位在极坐标图的中心,并沿着 y 轴定位另一个图像。

Is there anyway to add the image?无论如何要添加图像? It can be as-is, no transformation required.它可以原样,无需转换。

I'm using ggplot2 version 2.0我正在使用ggplot2 2.0 版

Gregor's suggestion for using the cowplot library has got me there.格雷戈尔关于使用cowplot库的建议让我到达了那里。

Following the introduction to cowplot you can use the draw_grob function to overlay images as you like.介绍了 cowplot之后,您可以使用draw_grob函数根据需要叠加图像。 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您能否将表格绘制到类似于非笛卡尔坐标的 annotation_custom 方法的 ggmap 上 - Can you plot a table onto a ggmap similar to annotation_custom method for non- Cartesian coordinates annotation_custom 与 ggplot2 中的 npc 坐标 - annotation_custom with npc coordinates in ggplot2 使用 imageR 将图像从笛卡尔坐标映射到极坐标 - Mapping image from cartesian to polar coordinates with imageR 使用stat_binhex在地图上的六角形热图给出错误:geom_hex()仅适用于笛卡尔坐标 - Hexagonal heatmap on map with stat_binhex gives error: geom_hex() only works with Cartesian coordinates R-将图添加到多面ggplot2对象(使用注解_自定义) - R - add plot to facetted ggplot2 object (using annotation_custom) 使用Annotation_custom()在指定坐标处绘制多个图 - Draw several plots at specified coordinates using annotation_custom() 无法使用带有注解_自定义的绘图实现ggplotly - Can't implement ggplotly on a plot with annotation_custom 如何将带有批注(annotation_custom)()的grob放置在绘图区域的精确区域? - How to place grobs with annotation_custom() at precise areas of the plot region? 由注解_自定义用geom_bar图创建的移动表 - Moving table created by annotation_custom with geom_bar plot R ggplot2:栅格grob的annotation_custom没有绘制 - R ggplot2: annotation_custom of raster grob does not plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM