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)
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
Dividing Each Cell in a Data Set by the Column Sum in R
Web Scraping of Key Stats in Yahoo! Finance with R
Group Vector on Conditional Sum
Using Grep to Subset Rows from a Data.Table, Comparing Row Content
Regex; Eliminate All Punctuation Except
How to Preserve Continuous (1,2,3,...N) Ranking Notation When Ranking in R
Counting Non-Blank Cells for Selected Columns
Include Text Control Characters in Plotmath Expressions
Download Plotly Using Downloadhandler
In R, How to Plot into a Memory Buffer Instead of a File
Read CSV with Two Headers into a Data.Frame
How to Pass Individual 'Curvature' Arguments in 'Ggplot2' 'Geom_Curve' Function