如何在R和ggplot2的背景图像上绘制图形 [英] how to fit the plot over a background image in R and ggplot2

查看:251
本文介绍了如何在R和ggplot2的背景图像上绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图适应在背景图片上显示的情节。由于锐度的降低,我无法将图像拉得太大。因此,我必须将情节拟合在比情节薄得多的背景图像上。我不知道怎么做。请参阅附图:





请忽略左边的彩色情节,这是一个我无法摆脱的独立情节,否则我不能显示'蜱'。它使用MatLab完成。我试图用R图替换MatLab图。 Matlab使用的TIFF图像重得多,但也更清晰。



感谢您的宝贵时间。

解决方案

您可以在 rasterGrob 中设置宽度和高度参数,它们都等于1npc,这将强制图像填充绘图区域。然后在保存图像时指定图像的高度和宽度,以获得所需的高宽比。主题和scale_y_reverse选项可用于控制轴的外观,如下所示。请注意,我们也可以使用展开参数来确保轴线不会比图像或数据更远。

  g < -  rasterGrob(img,width = unit(1,npc),height = unit(1,npc),interpolate = FALSE)

g_ct < - ggplot(data = df_ct)+
annotation_custom(g,-Inf,Inf,-Inf,Inf)+
geom_path(aes_string(x = df_ct $ X1,y = df_ct $ x0),color ='red',size = 1)+
scale_y_reverse(,
labels = c(min(df_ct $ X0),rep(,length(seq(min (df_ct $ X0),max(df_ct $ X0),max(df_ct $ X0),max(df_ct $ X0),5)) - 2),max(df_ct $ X0)),
breaks = seq ),
expand = c(0,0))+
主题(plot.margin =单位(c(5,5,5,5),mm),
轴。 line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_line(size = 1),
axis.ticks.length = unit( 5,'mm'))+
scale_x_continuous()
g_ct

ggsave('test.png',height = 5,width = 2,units ='in ')



一些数据:

  df_ct < -  data.frame(X0 = 0:100)
df_ct $ X1 = sin(df_ct $ X0)+ rnorm(101)



和背景图片:

  https:/ /i.stack.imgur.com/aEG7I.jpg 


I am trying to fit the plot that is displayed over the background image. I cannot stretch the image too much because of the loss of sharpness. Therefore, I have to fit the plot over the background image which is much thinner than the plot. I do not know how to do it. Please see the attached plot:

Here is the sample R Code:

library(ggplot2)
library(readxl)
library(jpeg)
library(grid)

# find the ".jpg" image files and parse them to get the depth ranges for each image
setwd('~/R/Image_Example')
image_file <- "C1_9195-9197.jpg"
# read in the image file
img <- readJPEG(image_file)
g <- rasterGrob(img, interpolate = FALSE)

# read in the CT data
df_ct <- read_excel("CT_Doris25_short.xlsx", col_names = FALSE)

g_ct <- ggplot(data=df_ct) +
  annotation_custom(g, xmin=min(df_ct$X1), xmax=max(df_ct$X1), ymin=-Inf, ymax=Inf) +
  geom_path(aes_string(x=df_ct$X1, y=df_ct$X0), color='cyan') +
  scale_y_reverse() +
  theme(plot.margin = unit(c(0,0,0,0), "lines"))

g_ct

As you can see,

  1. I am trying to adjust the xmin and xmax in annotation_custom. Is there a way where the plot shown in 'cyan' color can be fit perfectly over the 'width' of the background image? I do not mind if the graph is thinner and the X-axis is not shown. In fact, that is what I do when I assemble the plot with a number of other plots using gridExtra package.

  2. I would like to display the starting depth 9195 at the top of the image shown, and ending depth 9197 at the bottom of the image. Then I do not need the Y axis. I know how to suppress the the display of X axis and Y axis labels. So that is not a problem. I turned them on for clarification. Can that be done programmatically?

  3. As you can see that the ticks along the Y axis vary in steps of 0.5, i.e., 9195, 9195.5, 9196, ... and so on. I would like to make them go up (or down) in steps of 0.1 instead of 0.5. Also, I do not want to show the Y-axis labels, just ticks. The depth at the top of the image (9195) and the bottom (9197) is sufficient.

So it should look similar to the image shown below:

Please disregard the colorful plot to the left, that is a separate plot which I cannot get rid of, otherwise I cannot show the 'ticks'. It was done using MatLab. I am trying to replace the MatLab plot with an R plot. Matlab is using TIFF images which are much heavier but also much sharper. I am going with JPEG which is acceptable.

Thanks for your time.

解决方案

You can set width and height arguments in rasterGrob both equal to 1 "npc", which will force the image to fill the plot area. Then specify the height and width of the image when you save it to get the aspect ratio you desire. Theme and scale_y_reverse options can be used to control the appearance of the axes as demonstrated also below. note that we can also use the expand parameter to ensure that the axes do not extend further than the image or data.

g <- rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc"), interpolate = FALSE)

g_ct <- ggplot(data=df_ct) +
  annotation_custom(g, -Inf, Inf, -Inf, Inf) +
  geom_path(aes_string(x=df_ct$X1, y=df_ct$X0), color='red', size=1) +
  scale_y_reverse("", 
                  labels = c(min(df_ct$X0), rep("", length(seq(min(df_ct$X0), max(df_ct$X0), 5))-2),max(df_ct$X0)), 
                  breaks = seq(min(df_ct$X0), max(df_ct$X0), 5),
                  expand = c(0,0)) +
  theme(plot.margin = unit(c(5,5,5,5), "mm"),
        axis.line.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.line.y = element_blank(),
        axis.ticks.y = element_line(size = 1),
        axis.ticks.length = unit(5,'mm')) +
  scale_x_continuous("")
g_ct

ggsave('test.png', height=5, width = 2, units = 'in')

Some data:

df_ct <- data.frame(X0 = 0:100)
df_ct$X1 = sin(df_ct$X0) +rnorm(101)

and a background image:

https://i.stack.imgur.com/aEG7I.jpg

这篇关于如何在R和ggplot2的背景图像上绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆