gpt4 book ai didi

r - 尝试将图像添加到极坐标图中给出 "Error: annotation_custom only works with Cartesian coordinates"

转载 作者:行者123 更新时间:2023-12-02 09:38:03 24 4
gpt4 key购买 nike

我尝试遵循answer's given already用于将图像添加到绘图中,但在使用 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

理想情况下,我希望能够将旗帜图像定位在极坐标图的中心内,并沿 y 轴放置另一个图像。

有办法添加图片吗?它可以保持原样,无需转换。

我正在使用ggplot2版本2.0

最佳答案

Gregor 关于使用cowplot 库的建议让我实现了这一点。

遵循introduction to 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)

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)

enter image description here

关于r - 尝试将图像添加到极坐标图中给出 "Error: annotation_custom only works with Cartesian coordinates",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com