1

I would like to import images such as this , so that i can plot another graph over the image as described here and here.

the problem I am having is the graph is not a graphical object with a fixed url, but rather, created by code. I don't really understand the code behind the image, but have been unable to scrape recreate it using RCurl and XML.

I see two possible options: using R to launch a browser and save image as or processing properly the code, I imagine somehow like this

URL<-"http://
test<-htmlParese(getURL(url))
xpathSApply(

any thoughts?

Community
  • 1
  • 1
Rolf Fredheim
  • 300
  • 3
  • 9

1 Answers1

1

The link to the image you want to scrape is not an "xml image". It is simply a .png file. So, it suffices to save the image to a file, load it into R, and then put it on the plot. Something like this will get you there, but you'll need to play with it a bit to make it pretty.

library(png)
# use the URL from your post, or construct on-the-fly
url = "http://pulse.blogs.yandex.net/?size=small&charset=utf8&period=20120116-20130116&query0=%D0%BF%D1%83%D1%82%D0%B8%D0%BD"
download.file(url,destfile='/tmp/test.png',mode='wb')
xvals=rnorm(10)
yvals=rnorm(10)
# just set up an "empty" plot
plot(xvals,yvals,type='n')
r = readPNG('/tmp/test.png')
# read the help for rasterImage for details
rasterImage(r,-1,-1,1,1)
# plot the points over the image
points(xvals,yvals)
seandavi
  • 2,818
  • 4
  • 25
  • 52
  • Did it work for you? I get this error message Warning message: In download.file(url, destfile = "test.png") : downloaded length 33784 != reported length 200 – Rolf Fredheim Jan 17 '13 at 13:20
  • which is followed by the error: Error in readPNG("test.png") : file is not in PNG format. It seems the files downloaded like that are corrupted, and marginally larger than just using 'save as' – Rolf Fredheim Jan 17 '13 at 13:26
  • 1
    Code works perfectly, all it needed was `download.file(url,destfile='test.png',mode="wb")` – Rolf Fredheim Jan 17 '13 at 13:33
  • Edited to reflect Rolf's comment about mode='wb'. – seandavi Jan 17 '13 at 23:02