[英]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.