0

I"m trying to plot with a png as background as this post : so I used this code

library(png)

#Replace the directory and file information with your info
ima <- readPNG("C:\\Documents and Settings\\Bill\\Data\\R\\Data\\Images\\sun.png")

#Set up the plot area
plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y")

#Get the plot information so the image will fill the plot box, and draw it
lim <- par()
rasterImage(ima, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
grid()
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="white")

The issue is that I cannot find way to modify or to specify the width and height of the picture, in the display.

How to do this ?

Community
  • 1
  • 1
ranell
  • 683
  • 13
  • 29

1 Answers1

0

This is what I've done, it works for me. width= 1552 and height=112. Adding the ylim and xlim, resize the window and fix with and height in the rasterImage function fix the issue.

resize.win <- function(Width=6, Height=6)
{
    dev.off();
    windows(record=TRUE, width=Width, height=Height)
}
resize.win(1552,300)
library(png)
#Replace the directory and file information with your info
img <- readPNG("C:\\Users\\Bill\\Desktop\\plan.png")
height<-dim(img)[1]
width<-dim(img)[2]

#Set up the plot area
plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y", xlim=c(1,width),ylim=c(1,height))

#Get the plot information so the image will fill the plot box, and draw it
lim <- par()
rasterImage(img,0,0,width,height)
grid()
lines(c(19, 1.2, 1.4, 1.6, 1.8, 2.0), c(69, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="black")
ranell
  • 683
  • 13
  • 29