首页 文章

如何在ggplot中使用图像作为一个点?

提问于
浏览
33

有没有办法在ggplot2的散点图中使用特定的小图像作为点 . 理想情况下,我会想要根据变量调整图像大小 .

这是一个例子:

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(size = qsec, shape = factor(cyl)))

所以我基本上想知道是否有办法提供特定图像作为形状?

3 回答

  • 4

    这是一个显示光栅图像而不是点的极简主义geom,

    library(ggplot2)
    library(grid)
    
    ## replace by a named list with matrices to be displayed
    ## by rasterGrob
    .flaglist <- list("ar" = matrix(c("blue", "white", "blue"), 1), 
                      "fr" = matrix(c("blue", "white", "red"), 1))
    
    flagGrob <- function(x, y, country, size=1, alpha=1){
      grob(x=x, y=y, country=country, size=size, cl = "flag")
    }
    
    drawDetails.flag <- function(x, recording=FALSE){
    
      for(ii in seq_along(x$country)){
        grid.raster(x$x[ii], x$y[ii], 
                    width = x$size[ii]*unit(1,"mm"), height = x$size[ii]*unit(0.5,"mm"),
                    image = .flaglist[[x$country[[ii]]]], interpolate=FALSE)
      }
    }
    
    
    scale_country <- function(..., guide = "legend") {
      sc <- discrete_scale("country", "identity", scales::identity_pal(), ..., guide = guide)
    
      sc$super <- ScaleDiscreteIdentity
      class(sc) <- class(ScaleDiscreteIdentity)
      sc
    }
    
    GeomFlag <- ggproto("GeomFlag", Geom,
                        required_aes = c("x", "y", "country"),
                        default_aes = aes(size = 5, country="fr"),
    
                        draw_key = function (data, params, size) 
                        {
                          flagGrob(0.5,0.5, country=data$country,  size=data$size)
                        },
    
                        draw_group = function(data, panel_scales, coord) {
                          coords <- coord$transform(data, panel_scales)     
                          flagGrob(coords$x, coords$y, coords$country, coords$size)
                        }
    )
    
    geom_flag <- function(mapping = NULL, data = NULL, stat = "identity",
                          position = "identity", na.rm = FALSE, show.legend = NA, 
                          inherit.aes = TRUE, ...) {
      layer(
        geom = GeomFlag, mapping = mapping,  data = data, stat = stat, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes,
        params = list(na.rm = na.rm, ...)
      )
    }
    
    
    set.seed(1234)
    d <- data.frame(x=rnorm(10), y=rnorm(10), 
                    country=sample(c("ar","fr"), 10, TRUE), 
                    stringsAsFactors = FALSE)
    
    
    ggplot(d, aes(x=x, y=y, country=country, size=x)) + 
      geom_flag() + 
      scale_country()
    

    enter image description here

    (从ggflags包输出)

  • 13

    有一个名为 ggimage 的库可以做到这一点 . 见intro vignette here

    您只需要在 data.frame 中添加一个列,其中包含图像的地址,可以存储在Web上或本地计算机上,然后您可以使用 geom_image()

    library("ggplot2")
    library("ggimage")
    
    # create a df
    
    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)
                    )
    # plot2
      ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05)
    

    enter image description here

    PS . 请注意 ggimage 取决于EBImage . 所以要安装 gginamge 我必须这样做:

    # install EBImage
      source("https://bioconductor.org/biocLite.R")
      biocLite("EBImage")
    # install ggimage
      install.packages("ggimage")
    
  • 21

    首先,这是你的答案:

    为了向您展示如何使用如何更好地使用小部件来表示数据差异,我将在R图库中向您推荐chernoff faces的示例:

    alt text http://addictedtor.free.fr/graphiques/graphiques/graph_87.png

    生成此示例的所有代码均可在站点上获得 .

    或者,查看ggplot的stat_spoke以获取一个简单的小部件:alt text http://had.co.nz/ggplot2/graphics/706b1badf6469940342f204b7bc98857.png

    grImport提供了一种将简单的PDF图像导入绘图以用作点的机制 .

    现在跟随对你的例子的批评 .


    这不是散点图 . 它本质上是一个有序数据点的流动列表,其中颜色用于指示其中一个文本变量,并且已使用无信息和冗余的小部件来构建数据,但在大小或形状方面没有提供任何视觉反馈 .

    这不是一个好的图表,因为它完全无法回答所述的问题“是否会为更好的结果付出更多的代价”,并让读者自己挣扎得出这个结论(以及其他图表,必要时) .

    此外,作者还浪费了x,y轴 - 它们可以很好地用于通过传出和结果来定位元素,以提供对物有所值的视觉理解 . 相反,他们选择按人头成本与平均毕业率的比例来订购图标,这有点有用,但没有回答所述问题,也无法直接直观地比较大学之间的相对比率,或者成本与 Value 之间的关系 .

    正如我所说,在我看来,这是一个糟糕的图表,你的读者不会被你复制它 .

相关问题