How to use external image with R plot?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use external image with R plot?

Manish Gupta
This post was updated on .
Hi,

I am wokring on stacked bar plot and i need to add one arrow dynamically.






Final image:



I need to import image and position based on stack bar plot. How can i implement it?
Reply | Threaded
Open this post in threaded view
|

Re: How to use external image with R plot?

Michael Sumner-2
There are many ways to do this, here is one using an example from ?barplot

library(jpeg)
x <- readJPEG("arrow_glossy_right_red.jpg")
barplot(VADeaths, border = "dark blue")
rasterImage(x, 0, 60, 1, 80)

You'll have to change those numbers to suit your plot, par('usr")
gives a quick idea of the extents. You should be able to see that JPEG
is not a good format for drawings like this (and using images like
this is not a good method anyway since you never know what scale you
might want the shape to be drawn at). You might be better just using
?arrows or ?symbols but if you are bound to a particular image source
try to save it as a pixel format that doesn't crush the data (such as
PNG).

Also, I'm not really "merging images" here, and I wouldn't say that's
the way to go about it. This is using R's plot support for vector and
raster graphics.

Cheers, Mike.

On Tue, Jul 10, 2012 at 5:19 PM, Manish Gupta <[hidden email]> wrote:

> Hi,
>
> I am wokring on stacked bar plot and i need to add one arrow dynamically.
>
>
> http://r.789695.n4.nabble.com/file/n4635959/arrow_glossy_right_red.jpg
>
> http://r.789695.n4.nabble.com/file/n4635959/Screenshot.10.png
>
> Final image:
>
> http://r.789695.n4.nabble.com/file/n4635959/Screenshot.1.png
>
> Is there any packae which can be used for merging images?
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-use-external-image-with-R-plot-tp4635959.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Michael Sumner
Hobart, Australia
e-mail: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to use external image with R plot?

Manish Gupta
Hi,

I need to position arrow according to content of data dynamically. let's say i have vector c(2,3,56,9) and i need to put arrow at 56 dynamically.  How can i use above method.

Regards
Reply | Threaded
Open this post in threaded view
|

Re: How to use external image with R plot?

glsnow
You can use the locator function to retrieve the user coordinates of a
point that you click on in the plot, then use those points with
rasterImage to add the image.  So replace the last 2 lines of
Michael's answer with something like:

> barplot(VADeaths, border = "dark blue")
> tmp <- locator(1)
> rasterImage(x, tmp$x - 1, tmp$y - 10, tmp$x, tmp$y + 10)




On Tue, Jul 10, 2012 at 8:05 PM, Manish Gupta <[hidden email]> wrote:

> Hi,
>
> I need to position arrow according to content of data dynamically. let's say
> i have vector c(2,3,56,9) and i need to put arrow at 56 dynamically.  How
> can i use above method.
>
> Regards
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-use-external-image-with-R-plot-tp4635959p4636088.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.