简体   繁体   English

使用ggplot绘制带有图例的图像

[英]Use ggplot to plot over an image with legend

I would like to map OHS incidents over a PNG of a hospital floorplan using ggplot2. 我想使用ggplot2在医院平面图的PNG上映射OHS事件。 I have tried reading this non-geographic map in as a ggimage. 我尝试将这张非地理地图读为ggimage。

ICU平面图

So far I have tried the following with the dataset (14462) observations that I have. 到目前为止,我已经尝试使用数据集(14462)进行以下观察。

Example dataset 示例数据集

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))

I have tried reading the file in as a raster and then using ggmaps but the difficulty is no legend. 我曾尝试以光栅形式读取文件,然后使用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)

I would really like to use ggplot but need the legend. 我真的很想使用ggplot但需要图例。 I am not sure what other methods I can use to ggplot over a PNG. 我不确定我可以使用哪些其他方法对PNG进行ggplot。

The real "magic" for this is just fullpage=FALSE in the call to ggimage() , but you'll have to clean up axes a bit then: 真正的“魔术”只是对ggimage()的调用中的fullpage=FALSE ,但是您必须对轴进行一些清理,然后:

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())

在此处输入图片说明

You should prbly clean up the legend, too and I'd suggest lightening the base image a bit since it will be difficult to see the contrast between it and the colors you're going to overlay (unless they're large objects). 您也应该仔细清理图例,并且我建议您稍微淡化基础图像,因为很难看到其与要叠加的颜色之间的对比度(除非它们是大物体)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM