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

标签 r ggplot2 raster ggmap ggimage

我想使用 ggplot2 将 OHS 事件映射到医院平面图的 PNG 上。我曾尝试将这张非地理 map 作为 ggimage 阅读。

ICU Floor Plan

到目前为止,我已经尝试使用我拥有的数据集 (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())

enter image description here

您也应该清理图例,我建议稍微减轻基础图像的亮度,因为很难看到它与要叠加的颜色之间的对比度(除非它们是大型物体)。

关于r - 使用 ggplot 绘制带有图例的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290832/

相关文章:

r - 将颜色保存在 R 调色板中

r - ggplot barplot中y轴的顺序错误

r - 无法在 R 中的光栅堆栈上执行中位数()

r - 使用Rvest登录网站抓取403错误

r - 将因子的计数添加到数据帧

r - 如何将列名传递给处理 data.frames 的函数

r - 获取光栅 map 中补丁的坐标(R 中的光栅包)

将 ASC 文件读入 R

r - 将字符串向量转换为整数向量

r - R 中 dput() 的反义词是什么?