如何将图片插入到ggplot图表的每个单独的栏中 [英] How to insert pictures into each individual bar in a ggplot graph
本文介绍了如何将图片插入到ggplot图表的每个单独的栏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试比较不同统计数据的不同NBA新秀,并且我认为如果我可以在图的末尾添加球员的面孔,例如
I'm trying to compare different NBA rookies across different stats, and I thought the graph would look great if I could add the player's face at the end of the graph like in the r/dataisbeautiful graphs. My code is currently this:
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")
这是我的图形当前的外观
This is what my graph currently looks like
推荐答案
您没有提供reprex,所以我需要做些补充.我大概会这样.
You didn't provide a reprex, so I need to make something up. I would probably do it like this.
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)
Created on 2019-11-03 by the reprex package (v0.3.0)
这篇关于如何将图片插入到ggplot图表的每个单独的栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文