如何将图片插入到ggplot图表的每个单独的栏中


9

我正在尝试比较不同统计数据的不同NBA新秀,并且我认为如果可以像r / dataisbeautiful图中那样在图的末尾添加球员的面孔,该图将看起来很棒。我的代码当前是这样的:

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")

这是我的图形当前的样子喜欢


2
你有没有看到这个博客帖子,看起来相当相关:jcarroll.com.au/2019/08/13/ggtext-for-images-as-x-axis-labels

2
ggtext软件包似乎允许这样做:github.com/clauswilke/ggtext#markdown-in-theme-elements
Jon

这回答了你的问题了吗?在动画ggplot2中包含轴标签上的图像
Tjebo,

Answers:


7

您没有提供代表,所以我需要弥补。我可能会这样做。

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软件包(v0.3.0)创建于2019-11-03


谢谢,这很好!我想问一下是否可以通过以下方式在同一栏上显示两个图像(我假设是通过弄乱了hjust值):ggplot(data,aes(animal,count,fill = animal, image = image&x))
Pedro Guizar

请为此发布一个单独的顶级问题。
克劳斯威尔克


这是非常有用的。有计划在CRAN上使用ggtextures吗?
stevec

不。现在有了ggpattern,它的功能要强大得多。github.com/coolbutuseless/ggpattern
克劳斯·威尔克
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.