【问题标题】:Inserting an image to a bar chart in ggplot在ggplot中将图像插入条形图
【发布时间】:2020-03-19 20:24:47
【问题描述】:

我有三个组(a、b、c)及其关联值,我想将它们绘制为条形图。

使用 ggplot,我想在每个栏中插入不同的图像(以 png 格式)。所以在a栏中,我想插入图像'a.png',在b栏中,图像'b.png'和c中的图像'c.png'

df = data.frame(
  group = c('a', 'b', 'c'),
  value = 1:3)

ggplot(df, aes(group, value)) + 
  geom_col()

我发现的几篇文章没有帮助或很旧。

Inserting an image to ggplot outside the chart area

https://www.reddit.com/r/rstats/comments/6qh4kt/how_to_make_ggplot_not_beautiful_insert_pictures/

你有什么想法吗?

谢谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一种选择是使用ggimage。来源:https://guangchuangyu.github.io/pkgdocs/ggimage.html。试试这个:

    library(ggplot2)
    library(ggimage)
    library(dplyr)
    
    set.seed(1234)
    
    img <- list.files(system.file("extdata", package="ggimage"),
                      pattern="png", full.names=TRUE)
    
    df = data.frame(
      group = c('a', 'b', 'c'),
      value = 1:3,
      image = sample(img, size=3, replace = TRUE)
    ) %>% 
      mutate(value1 = .5 * value)
    
    ggplot(df, aes(group, value)) + 
      geom_col() + 
      geom_image(aes(image=image, y = value1), size=.2)
    

    reprex package (v0.3.0) 于 2020-03-19 创建

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      相关资源
      最近更新 更多