如何使用j?(或其他格式)图像作为直方图中的背景使用R? [重复]

时间:2022-09-04 21:21:21

Possible Duplicate:
Overlay data onto Background image in R

可能重复:将数据叠加到R中的背景图像上

I have seen nice graphics with a photo in the background. So it is possible?

我在背景中看到了漂亮的图片和照片。那有可能吗?

May I ask the experts how to place a photo, JPG, or if necessary other format, in the background of a classical histogram so that the borders touch the X and Y axes?

请问专家如何在经典直方图的背景中放置照片,JPG或必要的其他格式,以便边界接触X轴和Y轴?

Thank you.

谢谢。

1 个解决方案

#1


1  

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.

您可以使用rasterImage函数将栅格添加到现有图形中,然后它将作为添加在其上的任何内容的背景。请参阅@ mplourde评论中有关读取jpeg或其他图像格式的方法的链接,然后可以将其与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:

运行par('usr')将为您提供从轴到轴绘制的当前用户坐标,或者您可以使用grconvertX和grconvertY来查找其他坐标集。因此,对于直方图,您可以绘制直方图,然后使用rasterImage放置图像,然后使用add = TRUE再次使用hist:

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.

但是,要非常小心,背景图像不会分散直方图本身的注意力,可能会使图像褪色或添加Alpha通道以使其半透明可以提供帮助。

#1


1  

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.

您可以使用rasterImage函数将栅格添加到现有图形中,然后它将作为添加在其上的任何内容的背景。请参阅@ mplourde评论中有关读取jpeg或其他图像格式的方法的链接,然后可以将其与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:

运行par('usr')将为您提供从轴到轴绘制的当前用户坐标,或者您可以使用grconvertX和grconvertY来查找其他坐标集。因此,对于直方图,您可以绘制直方图,然后使用rasterImage放置图像,然后使用add = TRUE再次使用hist:

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.

但是,要非常小心,背景图像不会分散直方图本身的注意力,可能会使图像褪色或添加Alpha通道以使其半透明可以提供帮助。