簡體   English   中英

嘗試將圖像添加到極坐標圖會給出“錯誤:annotation_custom 僅適用於笛卡爾坐標”

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

我已經嘗試按照已經給出答案將圖像添加到繪圖中,但是在使用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 版

格雷戈爾關於使用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)

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM