Plot a Jpg Image Using Base Graphics in R

Plot a JPG image using base graphics in R

Instead of rimage, perhaps use the package ReadImages:

library('ReadImages')
myjpg <- read.jpeg('E:/bigfoot.jpg')
plot(myjpg)

plot jpg image in R language

Pascal's solution in the comments works great, here's what you can do, too:

jpeg('test.jpg')
plot(1,1)
dev.off()

Adding a picture to plot in R

You need to read your png or jpeg file through the png and jpeg packages. Then, with the rasterImage function you can draw the image on a plot. Say that your file is myfile.jpeg, you can try this:

require(jpeg)
img<-readJPEG("myfile.jpeg")
#now open a plot window with coordinates
plot(1:10,ty="n")
#specify the position of the image through bottom-left and top-right coords
rasterImage(img,2,2,4,4)

The above code will draw the image between the (2,2) and (4,4) points.

Print/Show JPG file in R

Here's some starter code you can build on:

library(rvest)
library(httr)
library(jpeg)

lego_movie <- html("http://www.imdb.com/title/tt1490017/")

poster <- lego_movie %>%
html_nodes("#img_primary img") %>%
html_attr("src")

GET(poster, write_disk("lego.jpg"))
img <- readJPEG("lego.jpg")
plot(1:2, type='n')
rasterImage(img, 1, 1.25, 1.1, 1)

How to add a jpeg logo to the top lhs outer margin of a base graphics plot?

You could set xpd=TRUE to clip the image to the figure region rather than to the plot region. Then in rasterImage() just think the coordinates beyond the edges. You'll have to play around a little with the par() and rasterImage() position vectors.

Example

par(oma=c(2, 0, 5, 0), xpd=TRUE)  # c(bottom, left, top, right)
plot(1:10, ty="n")
rasterImage(img, -0.5, 12, 3, 15) # c(xleft, ybottom, xright, ytop)

Sample Image

Image Data

library(png)
myurl <- "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a"
z <- tempfile()
download.file(myurl,z,mode="wb")
img <- readPNG(z)
file.remove(z)

How can i use a jpg (or other format) image as a background in a histogram using R?

You can use the rasterImage function to add a raster to an existing graph, it will then be the background for anything added on top of that. See the link in @mplourde's comment for ways to read jpeg or other image formats in that can then be used with rasterImage.

Running par('usr') will give you the current user coordinates to plot from axis to axis or you can use grconvertX and grconvertY to find other sets of coordinates. So for a histogram you could plot the histogram, then use rasterImage to place your image, then use hist again with add=TRUE:

tmp <- rnorm(100)
hist(tmp)
image <- as.raster(matrix(0:1, ncol=5, nrow=3))
tmp2 <- par('usr')
rasterImage(image, tmp2[1], tmp2[3], tmp2[2], tmp2[4])
hist(tmp, add=TRUE, border='red', lwd=3)

However, be very careful that the background image does not distract from the histogram itself, possibly fading your image or adding an alpha channel to make it semitransparent can help.



Related Topics



Leave a reply



Submit