![](/img/trans.png)
[英]Can you plot a table onto a ggmap similar to annotation_custom method for non- Cartesian coordinates
[英]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.