标签: ggimage

使用ggplot绘制带图例的图像

我想使用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.

r raster ggplot2 ggmap ggimage

5
推荐指数
1
解决办法
360
查看次数

Google 底图和 geom_sf 与 R 中的自定义 geom_image 符号和注释标签对齐

我想在放置在谷歌底图上的 geom_sf 多边形图层上使用自定义符号和注释。最初,我很难正确排列底图和多边形

未对齐的多边形图像

所以我使用了Stack Overflow 解决方案来修复它。现在底图和多边形图层对齐

在此输入图像描述

但我想要使用 geom_image 的自定义符号和使用注释的标签。未修改的底图和带有自定义符号的注释图层可以正常工作

带有符号和标签的未对齐多边形层

下面我扩展了 andyteucher 和 user1453488 的代码,并添加了我想要使用的 geom_image 和 annotate 函数。

我通过使用 sf 包进行绘图尝试了替代解决方案,但我无法弄清楚如何在具有纬度和经度的绘图上使用自定义符号。我的实际数据有纬度和经度。尽管我更熟悉 ggmap,但我愿意接受替代解决方案。

您知道如何将谷歌底图、具有精确投影的多边形图层、自定义符号(从具有地理坐标的数据框生成)和标签放在同一张地图上吗?谢谢。

# code modified and expanded from andyteucher's solution
library(ggplot2)
library(ggmap)
library(sf)
library(ggimage)

#load shapefile with sf package
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

# Transform nc to EPSG 3857 (Pseudo-Mercator, what Google uses)
nc_3857 <- st_transform(nc, 3857)

map <- get_map("north carolina", maptype = "satellite", zoom = 6, source = "google")

# display …
Run Code Online (Sandbox Code Playgroud)

r ggmap r-sf ggimage

5
推荐指数
0
解决办法
1179
查看次数

在 {ggimage} 中保持原始图像比例

我正在尝试在{ggimage}图层中使用非平方图像,但我似乎无法使比例适合原始图像比例。文档指向了这个asp论点,但这似乎不起作用。

这是一个明显的非方形图像:https : //ps.w.org/insert-headers-and-footers/assets/banner-772x250.png

以下是结果{ggimage}

library(ggimage)
library(ggplot2)
d <- data.frame(
  x = 1,
  y = 1,
  image = "https://ps.w.org/insert-headers-and-footers/assets/banner-772x250.png"
)
Run Code Online (Sandbox Code Playgroud)
ggplot(d, aes(x, y)) + 
  geom_image(aes(image=image), size = 1)

ggplot(d, aes(x, y)) + 
  geom_image(aes(image=image), size = 1, asp = 0.1)

ggplot(d, aes(x, y)) + 
  geom_image(aes(image=image), size = 1, asp = 0.5)

ggplot(d, aes(x, y)) + 
  geom_image(aes(image=image), size = 1, asp = 0.9)

Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

如何让图片保持原来的宽高比?

r ggimage

5
推荐指数
0
解决办法
156
查看次数

在 glot2 中使用 SVG 图像作为符号

我想使用存储在外部文件(例如 SVG)中的矢量图形作为图中的绘图符号ggplot2。例如,按照 grImport vignette 中的示例(图 8)https://cran.r-project.org/web/packages/grImport/vignettes/import.pdf

在此输入图像描述

此示例导入​​自定义 shapefile,然后使用lattice例如绘制它

xyplot(V8 ~ V7, data = flower, xlab = "Height",
               ylab = "Distance Apart",
               panel = function(x, y, ...) {
                                grid.symbols(PSflower, x, y, units = "native",                     
                                             size = unit(5, "mm"))})
Run Code Online (Sandbox Code Playgroud)

grid.symbols()来自grImport包并且是PSflower由 . 在别处导入的 Picture 对象grImport

ggimage包接近执行此操作,但它将图像转换为绘图下方的光栅,这是我试图避免的。

有什么方法可以实现类似的东西ggplot2吗?

标记

r ggplot2 grimport ggimage

5
推荐指数
1
解决办法
2940
查看次数

ggimage: Custom image from computer rather than from web

The following graph can be produced from the R code given below. The images used in the graph are fetched from website. I'm wondering how to use images from computer.

在此输入图像描述

library("ggplot2")
library("ggimage")

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                image = sample(c("https://www.r-project.org/logo/Rlogo.png",
                                 "https://jeroenooms.github.io/images/frink.png"),
                               size=10, replace = TRUE)
                )

ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05)
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggimage

4
推荐指数
1
解决办法
1993
查看次数

离线绘制Google静态地图上的地图坐标

历史记录:从静态Google地图png中提取的栅格数据,将其加载到R设备上ggimage.

library (png)
library (ggmap)

rasterArray <- readPNG ("My.png")

x = c (40.702147,40.718217,40.711614)
y = c (-74.012318,-74.015794,-73.998284)

myData <- data.frame (x, y)

print (ggimage (rasterArray, fullpage = TRUE, coord_equal = FALSE) 
    + geom_point (aes (x = x, y = y), data = myData, colour = I("green"), 
      size = I(5), fill = NA))
Run Code Online (Sandbox Code Playgroud)

我确实运行dput了,rasterArray但输出是20 MB,不能在这里发布.
顺便说一句,这是静态地图的URL:

问:为绘制"GPS坐标"包含的像素地图将R设备上,我需要scaledata.frame

我看到这个页面:http://www-personal.umich.edu/~varel/rdatasets/Langren1644.html 我是否需要按照他们在这里展示的方式进行缩放?

如果是,那么除了scale函数的手册之外的其他内容我还需要了解才能完成这项工作吗?

我在错误的树上吠叫吗?

png r ggplot2 google-maps-static-api ggimage

3
推荐指数
1
解决办法
5313
查看次数

可以使用ggimage软件包中的geom_image()来保留图像长宽比吗?

在研究此答案时,我尝试geom_image()ggimage包中绘制图像带,但无法使其正常工作。geom_image()修改图像的宽高比,但我不知道如何阻止它(或者是否有可能)。我也不清楚用什么单位size来衡量。从代码的行为方式来看,也许它在npc从0到1的坐标范围内,而不管ggplot2坐标系如何?

这是我使用的代码:

require(ggimage)
df_img <- data.frame(phase = c("Interphase", "Prophase", "Metaphase", "Anaphase", "Telophase"),
                     image = c("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_interphase.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_prophase.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_metaphase2.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_anaphase2.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_telophase.jpg"))
df_img$phase <- factor(df_img$phase, levels=df_img$phase)

ggplot(df_img, aes(x = phase, y = 0, image = image)) + geom_image(size = 0.18)
Run Code Online (Sandbox Code Playgroud)

这是结果图像:

在此处输入图片说明

这是图像的外观:

在此处输入图片说明

注意:这是一个专门关于ggimage行为的问题。我知道如何使用其他方法来生成正确的图像,例如通过使用draw_image()Cowplot。

r ggplot2 ggimage

2
推荐指数
1
解决办法
848
查看次数

如何使用magick软件包删除图像背景?

是否可以通过magick包装去除图像的背景?

我知道如何在Gimp / Inkscape中使用边缘检测来裁剪轮廓。但是,我希望使用来自动处理大量图像R

我的最终目标是使用该ggimage程序包将这些图像绘制为x,y坐标,但是这些图像的背景当前与该绘制重叠(与芬克相比,这只狗是狗)

library("ggplot2")
library("ggimage")

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                image = sample(c("http://www.supercoloring.com/sites/default/files/silhouettes/2015/05/cairn-terrier-black-silhouette.svg", "https://jeroenooms.github.io/images/frink.png"),
                               size=10, replace = TRUE)
                )

ggplot(d, aes(x, y)) + geom_image(aes(image=image))
Run Code Online (Sandbox Code Playgroud)

输出

可以使用image magick的图像修剪图像的边缘 image_trim()

img <- image_read_svg("http://www.supercoloring.com/sites/default/files/silhouettes/2015/05/cairn-terrier-black-silhouette.svg")
image_trim(img)
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的。

有任何想法吗?

r imagemagick ggimage

1
推荐指数
1
解决办法
1098
查看次数