我想使用 ggplot2 将 OHS 事件映射到医院平面图的 PNG 上。我曾尝试将这张非地理 map 作为 ggimage 阅读。
到目前为止,我已经尝试使用我拥有的数据集 (14462) 观察进行以下操作。
示例数据集
toy <- data.frame(patient_ID = c(1001,1001,1002,1003,1004),
SEX = c("M","M","F","F","F"),
phenotype = c("Psychiatric","Psychiatric", "Cardio",
"Cancer","Psychiatric"),
x = c(0.5737, 0.5737, 0.6968, 0.4704, 0.6838),
y= c(0.3484, 0.3484, 0.62, 0.5216, 0.2486))
我曾尝试将文件作为光栅读取,然后使用 ggmaps,但难度并不大。
library(ggmap)
library(png)
library(ggplot2)
library(data.table)
toy <- fread("toy.csv")
# read in image
r <- readPNG("ICU-crop.png")
#use ggimage to convert to plot and add gender
ggimage(r, scale_axes = TRUE) +
geom_point(aes(x = x, y = y, colour = SEX), data = toy,
size = I(1), fill = NA)
我真的很想使用 ggplot 但需要图例。我不确定我可以使用哪些其他方法在 PNG 上进行 ggplot。
最佳答案
真正的“魔法”就是 fullpage=FALSE
在调用 ggimage()
,但是您必须稍微清理一下轴:
ggimage(r, fullpage = FALSE, scale_axes = TRUE) +
geom_point(aes(x = x, y = y, colour = SEX),
data = toy, size = I(1), fill = NA) +
labs(x=NULL, y=NULL) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank())
您也应该清理图例,我建议稍微减轻基础图像的亮度,因为很难看到它与要叠加的颜色之间的对比度(除非它们是大型物体)。
关于r - 使用 ggplot 绘制带有图例的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290832/