使用ggplot在R中创建图像填充图表 [英] Create an image filled chart in R using ggplot

查看:121
本文介绍了使用ggplot在R中创建图像填充图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建如下图所示的图表; esentailly表示一个百分比的填充形状(很可能是人的图像,但这在理论上可以是任何东西)。



我设法做到了这一点,尽管有很多条形图。但是有没有办法在R中做到这一点,最好使用ggplot?



我已经阅读了类似的问题



请注意,我从Google图片搜索中偷了这张图片。

解决方案

您真的只需要修改@PoGibas链接的答案中的绘图命令:此处





 库(png)
库(ggplot2)

性别选择<-read.table(text =
性别频率
F 70
M 30
,标头= T)
pcts< ;-round(prop.table(genderselection $ Freq)* 100)

#从imgur加载png文件为二进制
con<-url( https://i.imgur。 com / vFDSFYX.png,
open ='rb')
rawpng<-readBin(con,what ='raw',n = 50000)
close(con)

img<-readPNG(rawpng)
h<-dim(img)[1]
w<-dim(img)[2]

#找到脚开始和头结束的行
pos1<-其中(apply(im g [,, 1],1,函数(y)any(y == 1)))
mn1<-min(pos1)
mx1<-max(pos1)
pospctM<-round((mx1-mn1)* pcts [2] / 100 + mn1)
pospctF<-round((mx1-mn1)* pcts [1] / 100 + mn1)

#根据百分比用不同的颜色填充主体
#请注意,这取决于png是位图的事实。
#png表示为矩阵,每个像素
#带有一个单元,r,g,b分为3层。
dim(img)
#> [1] 360360 3

#仅取红色值
#创建2d矩阵#图像为黑白,因此黑色对应于0
#白色对应于1。然后将
#的值更改为与三个类别之一相对应的单元格。
imgmtx<-img [h:1,,1]
whitemtx<--(imgmtx == 1)
colmtx<-matrix(rep(FALSE,h * w), nrow = h)
midpt<-轮(w / 2)-10
colmtx [mx1:pospctM,1:midpt]<-真
colmtx [mx1:pospctF,(midpt +1):w]<-TRUE
imgmtx [whitemtx& colmtx]<-0.5

#需要将矩阵融化为ggplot可以理解的数据.frame
df<-reshape2 :: melt(imgmtx)
head( df)
#> Var1 Var2 value
#> 1 1 1 0
#> 2 2 1 0
#> 3 3 1 0
#> 4 4 1 0
#> 5 5 1 0
#> 6 6 1 0

cols <-c(rgb(255,255,255,maxColorValue = 255),
rgb(209,230,244,maxColorValue = 255),
rgb(42,128,183,maxColorValue = 255))

#然后使用3种颜色的背景热图和百分比填充
#将填充值转换为因子会导致离散比例。
#geom_tile包含三列:x,y,与
#x坐标,y坐标和单元格颜色相对应的填充。
ggplot(df,aes(x = Var2,y = Var1,fill = factor(value)))+
geom_tile()+
scale_fill_manual(values = cols)+
theme(legend.position = none)


I'm trying to create a chart like the below image; essentailly its a filled shape denoting a percentage (most likely an image of a human, but this could be anything theortically).

I've managed to do this is Excel, albeit with a lot of botching of bar charts. But is there a way to do this in R, preferably using ggplot?

I've read the similar question Use an image as area fill in an R plot which doesn't quite do the same thing, but I cannot envisage a solution using this method.

Any help is appreciated!

EDIT:

As pointed out, this has been answered using plotly: Use a custom icon in plotly's pie chart. Is this possible to do with ggplot?

Note I stole this image from a google image search for infographics.

解决方案

You really only need to modify the plotting command from the answer that @PoGibas linked: here


library(png)
library(ggplot2)

genderselection <- read.table(text="
  Gender Freq
                              F   70
                              M   30
                              ", header=T)
pcts <- round(prop.table(genderselection$Freq)*100)

# Load png file from imgur as binary
con <- url("https://i.imgur.com/vFDSFYX.png",
           open='rb')
rawpng <- readBin(con, what='raw', n=50000)
close(con)

img <- readPNG(rawpng)
h <- dim(img)[1]
w <- dim(img)[2]

# Find the rows where feet starts and head ends
pos1 <- which(apply(img[,,1], 1, function(y) any(y==1)))
mn1 <- min(pos1)
mx1 <- max(pos1)
pospctM <- round((mx1-mn1)*pcts[2]/100+mn1)
pospctF <- round((mx1-mn1)*pcts[1]/100+mn1)

# Fill bodies with a different color according to percentages
# Note that this relies on the fact that the png is a bitmap.
# The png is expressed as a matrix with a cell for each pixel
# and 3 layers for r,g,b.
dim(img)
#> [1] 360 360   3

# Create a 2d matrix by just taking the red values
# Image is black and white so black corresponds to 0
# white corresponds to 1. Then change the values of
#  the cells to correspond to one of three categories.
imgmtx <- img[h:1,,1]
whitemtx <- (imgmtx==1)
colmtx <- matrix(rep(FALSE,h*w),nrow=h)
midpt <- round(w/2)-10
colmtx[mx1:pospctM,1:midpt] <- TRUE
colmtx[mx1:pospctF,(midpt+1):w] <- TRUE
imgmtx[whitemtx & colmtx] <- 0.5

# Need to melt the matrix into a data.frame that ggplot can understand
df <- reshape2::melt(imgmtx)
head(df)
#>   Var1 Var2 value
#> 1    1    1     0
#> 2    2    1     0
#> 3    3    1     0
#> 4    4    1     0
#> 5    5    1     0
#> 6    6    1     0

cols <- c(rgb(255,255,255,maxColorValue = 255),
          rgb(209,230,244,maxColorValue = 255), 
          rgb(42,128,183,maxColorValue = 255))

# Then use a heatmap with 3 colours for background, and percentage fills
# Converting the fill value to a factor causes a discrete scale.
# geom_tile takes three columns: x, y, fill corresponding to 
# x-coord, y-coord, and colour of the cell.
ggplot(df, aes(x = Var2, y = Var1, fill = factor(value)))+
  geom_tile() +
  scale_fill_manual(values = cols) +
  theme(legend.position = "none")

这篇关于使用ggplot在R中创建图像填充图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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