【问题标题】:Create an image filled chart in R using ggplot [duplicate]使用ggplot在R中创建一个图像填充图表[重复]
【发布时间】:2018-01-30 13:14:12
【问题描述】:

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

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

我已经阅读了类似的问题Use an image as area fill in an R plot,它并没有完全做同样的事情,但我无法设想使用这种方法的解决方案。

感谢任何帮助!

编辑:

正如所指出的,这已使用 plotly 回答:Use a custom icon in plotly's pie chart这可能与ggplot有关吗?

注意我从谷歌图片搜索信息图表中窃取了这张图片。

【问题讨论】:

  • 谢谢。已编辑以仅反映 ggplot 作为答案会很好,但是有情节的解决方案非常好!
  • 如果你特别想要 ggplot,可能更好的副本是:Use an image as area fill in an R plot

标签: r ggplot2 data-visualization


【解决方案1】:

你真的只需要修改@PoGibas链接的答案中的绘图命令: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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 2021-08-16
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多