在装有标志(或图像)的ggplot2中创建条形图 [英] Create barplots in ggplot2 filled with flags (or images)

查看:62
本文介绍了在装有标志(或图像)的ggplot2中创建条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ggplot2 创建一个条形图,其中每个条形都代表一个国家.我希望每个小节的填充物成为该国的国旗,以便于识别.

I'm creating a barplot using ggplot2 where each bar represents a country. I would like the fill of each bar to be the country's flag for easy identification.

这是一个简单的例子

library("ggplot2")
DF <- data.frame(country = c("UK", "France", "Germany"), 
                 value=c(4, 3, 7))
ggplot(DF, aes(x=country, y=value, fill="#f00")) + geom_bar(stat="identity")

我希望条形为彩色/标志图像,而不是红色的条形.有人对此有想法/建议吗?

Instead of red bars I would like the bars to be coloured/images of flags. Does anyone have ideas/suggestions for doing this?

推荐答案

这里的可能方法是散点图,而不是条形图-仍然希望对您有所帮助.ggflags软件包( https://github.com/YTLogos/ggflags )geom_flag显示一个圆形的使用ggplot美学x,y和国家来标记图像.

The possible approach here is for a scatter plot, not a bar plot - still I hope you find it helpful. The ggflags package (https://github.com/YTLogos/ggflags) geom_flag displays a circular, sizeable flag image using ggplot aesthetics x, y and country.

devtools::install_github("YTLogos/ggflags") 

library("ggplot2")
library(ggflags)
DF <- data.frame(country = c("UK", "France", "Germany"), 
                 value=c(4, 3, 7))
ggplot(DF, aes(x=country, country=country, y=value)) + geom_flag(size = 10)

此处出于可见性考虑,将静态大小选择为10,但也可以将其设置为可变大小.

A static size of 10 is chose here just for visibility but it can be a variable size as well.

这篇关于在装有标志(或图像)的ggplot2中创建条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆