R:geom_image因coord_fixed变形

时间:2018-08-05 21:00:30

标签: r ggplot2 ggimage

我正在尝试将图像放置到需要具有固定坐标的绘图中(x,y值是GPS坐标,我希望地图正确缩放)。如果x和y的范围不匹配,则图像将被平坦化。

我不知道这是错误还是所需的行为。有没有一种方法可以使图像具有原始的长宽比?我想到的唯一一件事就是将不可见的点放在角落上,以使图再次成为正方形。

简单的示例如下:

require(tidyverse)
require(ggimage)

plot_image <- function(x_size, y_size) {

  dta_points <- crossing(x = c(-x_size, x_size), y = c(-y_size, y_size))
  dta_img <- data_frame(x = 0, y = 0, image = 'https://www.r-project.org/logo/Rlogo.png')

  ggplot(NULL, aes(x, y)) + 
    geom_point(data = dta_points) +
    geom_image(data = dta_img, aes(image = image), size = 1) +
    ggtitle(paste0('x_size: ', x_size, ', y_size: ', y_size)) +
    coord_fixed()
}

plot_image(x_size = 1, y_size = 1)
plot_image(x_size = 0.1, y_size = 1)
plot_image(x_size = 1, y_size = 0.1)

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以在public protocol MyProtocol: AnyObject {} public class MyClass: Subjectable { public var observers = [MyProtocol]() public func removeObserver(_ observer: MyProtocol) { observers = observers.filter { $0 === observer } } } 内调整xmin/xmax/ymin/ymax自变量以将图像放置在任意位置,同时确定图像的纵横比:

annotation_custom

示例:

library(ggplot2)
library(png)
library(grid)

download.file("https://www.r-project.org/logo/Rlogo.png", 'Rlogo.png', mode = 'wb')
image <- readPNG("Rlogo.png")
logo <- rasterGrob(image, interpolate = TRUE)

ggplot() + 
  annotation_custom(logo, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) 

发表您的评论:

您只需要指定ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + annotation_custom(logo, xmin = 7, xmax = 8, ymin = 2, ymax = 4) + geom_point() x美观度即可保留原始的长宽比,并且y将在需要时帮助缩放:

size

enter image description here

还请签出library(ggimage) dta_img <- data.frame(x = 6, y = 3, image = 'https://www.r-project.org/logo/Rlogo.png') ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + geom_image(data = dta_img, aes(x, y, image = image), size = 0.1) + geom_point() ,它具有内置功能,可以创建与您链接的地图相似的精美地图:Leaflet for R

答案 1 :(得分:1)

尝试geom_custom,

RAW DATA
ID| Start Date Time     | End Date Time
1 | 2018-08-06 13:00:00.000 | 2018-08-07 10:00:00.000
2 | 2018-08-10 08:00:00.000 | 2018-08-10 15:00:00.000

RESULT
ID| Start Date Time     | End Date Time
1 | 2018-08-06 13:00:00.000 | 2018-08-06 23:59:59.000
1 | 2018-08-07 00:00:01.000 | 2018-08-07 10:00:00.000
2 | 2018-08-10 08:00:00.000 | 2018-08-10 15:00:00.000