【问题标题】:How to insert pictures into each individual bar in a ggplot graph如何将图片插入到ggplot图中的每个单独的条中
【发布时间】:2019-11-04 01:37:50
【问题描述】:

我正在尝试比较不同 NBA 新秀在不同统计数据上的表现,我认为如果我可以像 r/dataisbeautiful graphs 那样在图表末尾添加球员的脸,图表会看起来很棒。 我的代码目前是这样的:

a3 %>%
  ggplot(aes(x = reorder(Player,
                         PPM),
             y = PPM)) +
  geom_bar(stat = "identity",
           aes(fill = Player)) +
  geom_text(aes(label = PPM), size = 3, position = position_dodge(width = 1),
            hjust = -0.1) +
  coord_flip() +
  theme_minimal() +
  xlab("Player") +
  ylab("Points Per Minute") +
  theme(legend.position = "none")

这是我的图表目前的样子

【问题讨论】:

标签: r image ggplot2


【解决方案1】:

你没有提供一个reprex,所以我需要弥补一些东西。我可能会这样做。

library(tidyverse)
library(ggtextures)
library(magick)
#> Linking to ImageMagick 6.9.9.39
#> Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

data <- tibble(
  count = c(5, 6, 6, 4, 2, 3),
  animal = c("giraffe", "elephant", "horse", "bird", "turtle", "dog"),
  image = list(
    image_read_svg("http://steveharoz.com/research/isotype/icons/giraffe.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/elephant.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/horse.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/bird.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/turtle.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/dog.svg")
  )
)

ggplot(data, aes(animal, count, fill = animal, image = image)) +
  geom_isotype_col(
    img_height = grid::unit(1, "null"), img_width = NULL,
    ncol = 1, nrow = 1, hjust = 1, vjust = 0.5
  ) +
  coord_flip() +
  guides(fill = "none") +
  theme_minimal()

reprex package (v0.3.0) 于 2019 年 11 月 3 日创建

【讨论】:

  • 谢谢,效果很好!我想问一下是否可以通过以下方式在同一个栏上显示两个图像(我假设通过弄乱 hjust 值): ggplot(data, aes(animal, count, fill = animal,图像 = 图像 & x))
  • 请为此发布一个单独的顶级问题。
  • 这非常有用。是否有计划在 CRAN 上获取 ggtextures?
  • 没有。现在有更强大的ggpattern。 github.com/coolbutuseless/ggpattern
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
相关资源
最近更新 更多