如何在ggsurvplot中插入一张图片?

huangapple go评论75阅读模式
英文:

How to insert an image inside a ggsurvplot?

问题

I would like to insert an image inside/over a ggsurvplot.
如何在 ggsurvplot 中插入图像?

How can I achieve this?
如何实现这个目标?

Let's draw a minimal working example of a ggsurvplot:
让我们绘制一个 ggsurvplot 的最小工作示例:

How can I insert an image on this ggsurvplot, for example, the R_logo to produce something like this (images assemble with Powerpoint)?
如何在这个 ggsurvplot 上插入图像,例如 R_logo,以产生类似下面这样的效果(使用 Powerpoint 组装的图像)?

I know how to do it with "standard" ggplot but not with ggsurvplot.
我知道如何在“标准” ggplot 中实现,但不知道如何在 ggsurvplot 中实现。

Any clue?
有什么线索吗?

Thank you in advance for your help.
提前感谢您的帮助。

Charles
查尔斯

英文:

I would like to insert an image inside/over a ggsurvplot.
How can I achieve this?

Let's draw a minimal working example of a ggsurvplot:

require (survminer)
require (survival)
fit2 <- survfit (Surv (entry, futime, death) ~ 1, myeloma)
ggsurvplot (fit2,
            data = myeloma,
            ggtheme = theme_gray(),
            legend = "none",
            risk.table = TRUE)

如何在ggsurvplot中插入一张图片?

How can I insert an image on this ggsurvplot, for a example the R_logo to produce something like this (images assemble with Powerpoint) ?

如何在ggsurvplot中插入一张图片?

I know how to do it with "standard" ggplot but, not with ggsurvplot.

Any clue ?

Thank you in advance for your help.

Charles

答案1

得分: 2

以下是您要翻译的部分:

"要在您的生存图中添加图像的一种选项是使用 ggplot2::annotation_custom。与默认的 ggplot 相比,唯一的区别是 ggruvplot 是一个列表,其中将绘图存储为名为 plot 的元素。因此,我们必须将图像添加到此列表元素中:"

require(survminer)
require(survival)
library(ggplot2)

fit2 <- survfit(Surv(entry, futime, death) ~ 1, myeloma)

gg <- ggsurvplot(fit2,
  data = myeloma,
  ggtheme = theme_gray(),
  legend = "none",
  risk.table = TRUE
)

fn <- tempfile(pattern = ".png")
download.file("https://www.r-project.org/logo/Rlogo.png", fn)
image <- png::readPNG(fn)
gg$plot <- gg$plot +
  annotation_custom(
    grob = grid::rasterGrob(
      image = image,
      x = unit(1, "npc") - unit(1, "mm"),
      y = unit(1, "npc") - unit(1, "mm"),
      vjust = 1, hjust = 1,
      width = unit(.25, "npc"),
      height = unit(.25, "npc")
    )
  )

gg

"要在图像周围添加黑色边框的一种选项是通过第二个 annotation_custom 在图像上添加一个 rectGrob。"

gg$plot <- gg$plot +
  annotation_custom(
    grob = grid::rasterGrob(
      image = image,
      x = unit(1, "npc") - unit(1, "mm"),
      y = unit(1, "npc") - unit(1, "mm"),
      vjust = 1, hjust = 1,
      width = unit(.25, "npc"),
      height = unit(.25, "npc")
    )
  ) +
  annotation_custom(
    grob = grid::rectGrob(
      x = unit(1, "npc") - unit(1, "mm"),
      y = unit(1, "npc") - unit(1, "mm"),
      vjust = 1, hjust = 1,
      width = unit(.25, "npc"),
      height = unit(.25, "npc"),
      gp = grid::gpar(col = "black", fill = NA)
    )
  )

gg

希望这些翻译对您有所帮助。

英文:

One option to add an image to your survival plot would be to use ggplot2::annotation_custom. The only difference compared to a default ggplot is that a ggruvplot is a list where the plot is stored as an element named plot. Hence, we have to add the image to this list element:

require(survminer)
require(survival)
library(ggplot2)

fit2 &lt;- survfit(Surv(entry, futime, death) ~ 1, myeloma)

gg &lt;- ggsurvplot(fit2,
  data = myeloma,
  ggtheme = theme_gray(),
  legend = &quot;none&quot;,
  risk.table = TRUE
)

fn &lt;- tempfile(pattern = &quot;.png&quot;)
download.file(&quot;https://www.r-project.org/logo/Rlogo.png&quot;, fn)
image &lt;- png::readPNG(fn)
gg$plot &lt;- gg$plot +
  annotation_custom(
    grob = grid::rasterGrob(
      image = image,
      x = unit(1, &quot;npc&quot;) - unit(1, &quot;mm&quot;),
      y = unit(1, &quot;npc&quot;) - unit(1, &quot;mm&quot;),
      vjust = 1, hjust = 1,
      width = unit(.25, &quot;npc&quot;),
      height = unit(.25, &quot;npc&quot;)
    )
  )

gg

如何在ggsurvplot中插入一张图片?<!-- -->

EDIT One option to add a black frame around the image would be to add a rectGrob on top of the image via a second annotation_custom.

gg$plot &lt;- gg$plot +
  annotation_custom(
    grob = grid::rasterGrob(
      image = image,
      x = unit(1, &quot;npc&quot;) - unit(1, &quot;mm&quot;),
      y = unit(1, &quot;npc&quot;) - unit(1, &quot;mm&quot;),
      vjust = 1, hjust = 1,
      width = unit(.25, &quot;npc&quot;),
      height = unit(.25, &quot;npc&quot;)
    )
  ) +
  annotation_custom(
    grob = grid::rectGrob(
      x = unit(1, &quot;npc&quot;) - unit(1, &quot;mm&quot;),
      y = unit(1, &quot;npc&quot;) - unit(1, &quot;mm&quot;),
      vjust = 1, hjust = 1,
      width = unit(.25, &quot;npc&quot;),
      height = unit(.25, &quot;npc&quot;),
      gp = grid::gpar(col = &quot;black&quot;, fill = NA)
    )
  )

gg

如何在ggsurvplot中插入一张图片?

huangapple
  • 本文由 发表于 2023年4月13日 17:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定