英文:
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)
How can I insert an image on this ggsurvplot, for a example the R_logo to produce something like this (images assemble with Powerpoint) ?
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 <- 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
<!-- -->
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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论