从jpeg中提取十六进制颜色,存储在data.frame中,然后使用ggplot进行绘图

问题描述 投票:0回答:1

就像标题所说的那样。我有一个图像,我想提取颜色的十六进制值,存储在data.frame中,然后使用ggplot绘图。

这就是我所拥有的:

library(ggplot2)
aws.logo = 'https://eventil.s3.amazonaws.com/uploads/group/avatar/8626/medium_highres_470196509.jpeg'

temp = tempfile()
download.file(aws.logo, temp, mode = 'wb')

## matrix of colors
y = jpeg::readJPEG(temp)
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
aws.img <- matrix(val, dim(y)[1], dim(y)[2])

out = reshape2::melt(aws.img)
names(out) = c('col', 'row', 'value')
out$value = as.character(out$value)

ggplot(out) + 
  geom_point(aes(x = row, y = -col), color = out$value) + ## why do I have to flip the order of 
  theme(legend.position="none")                           ## col after reshaping?

为什么颜色与网站上的徽标不同?

r ggplot2 jpeg
1个回答
0
投票

raedJPEG将返回0/1比例的RGB颜色值,而不是0/255比例。使用

val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)

此外,通常最好不要在ggplot调用中进行任何$操作。这样的事情会更安全

ggplot(out) + 
  geom_point(aes(x = row, y = -col, color = value)) +       
  theme(legend.position="none") + 
  scale_color_identity ()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.