使用ggplot绘制带图例的图像

mon*_*nes 5 r raster ggplot2 ggmap ggimage

我想使用ggplot2将OHS事件映射到医院平面图的PNG上.我曾尝试以ggimage的形式阅读这张非地理地图.

ICU平面图

到目前为止,我已经尝试了以下数据集(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))
Run Code Online (Sandbox Code Playgroud)

我曾尝试将文件作为光栅读取,然后使用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)
Run Code Online (Sandbox Code Playgroud)

我真的很想使用ggplot但需要传说.我不知道我可以使用什么其他方法来ggplot PNG.

hrb*_*str 5

真正的"魔力"只是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())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

你也应该清理传说,我建议稍微减轻基本图像,因为很难看到它与你要覆盖的颜色之间的对比(除非它们是大对象).