2012-09-28 11 views
14

मैं वेब से एक छवि पढ़ना चाहता हूं। जैसेमैं एक ggplot पर एक छवि ओवरले कैसे करूँ?

http://api.altmetric.com/donut/502878_64x64.png

और एक ggplot

df <- data.frame(x=1:10, y=sample(1:100,10)) 
# a fake plot to try it on. 
ggplot(df, aes(x,y)) + geom_point(size = 2) 

मैं यह कर होगा कैसे के ऊपरी दाएँ भाग में डालें?

+0

'annotation_raster':बस भयानक पैकेज Magick है, जो भी एक GIF ggplot छवियों पर आच्छादित करने की अनुमति देगा से एक अद्यतन जोड़ने। उदाहरण देखें [यहां] (http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf) – mnel

+0

धन्यवाद @mnel! मुझे अभी भी 'readotNG' और 'download.file (..., mode = 'wb' के साथ 'annotation_raster' – Maiasaura

उत्तर

18

आप annotation_raster लिए देख रहे हैं और readPNG

mypngfile <- download.file('http://api.altmetric.com/donut/502878_64x64.png', destfile = 'mypng.png', mode = 'wb') 
library(png) 
mypng <- readPNG('mypng.png') 


p <- qplot(mpg, wt, data = mtcars) + theme_bw() 
p + annotation_raster(mypng, ymin = 4.5,ymax= 5,xmin = 30,xmax = 35) + 
    geom_point() 

enter image description here

इन नई सुविधाओं (और अधिक उदाहरण) में वर्णित हैं here

+0

+1 से इस छवि को पढ़ने में सक्षम होना चाहिए। – Maiasaura

+0

' पर इंगित करने के लिए URL – mnel

3

सही समाधान यह था:

# This was one of my issues, reading a png from the web 
my_image <- readPNG(getURLContent('http://path.to/image.png')) 
p1 + annotation_raster(my_image, ymin = 4,ymax= 5,xmin = 30,xmax = 40) 
1
library(ggplot2) 
library(magick) 
library(here) # For making the script run without a wd 
library(magrittr) # For piping the logo 

# Make a simple plot and save it 
ggplot(mpg, aes(displ, hwy, colour = class)) + 
    geom_point() + 
    ggtitle("Cars") + 
    ggsave(filename = paste0(here("/"), last_plot()$labels$title, ".png"), 
     width = 5, height = 4, dpi = 300) 

# Call back the plot 
# Now call back the plot 
background <- image_read(paste0(here("/"), "Cars.png")) 
# And bring in a logo 
logo_raw <- image_read("https://i.imgur.com/e1IneGq.jpg") 

frames <- lapply(logo_raw, function(frame) { 
    image_composite(background, frame, offset = "+70+800") 
}) 

animation <- image_animate(image_join(frames)) 

image_write(animation, "~/Cars_Travolta.gif") 
संबंधित मुद्दे