用ggplot画你们心中的女神
用ggplot画你们心中的女神
注意文件名为英文;图形格式为jpg。
# Packages (for the entire project)
library(imager) # image loading and processing
# library(BiocInstaller)
# biocLite("ggvoronoi" )
library(dplyr) # data manipulation
library(ggplot2) # data visualization
library(tidyr) # data wrangling
library(ggvoronoi) # visualization
# Load an image into R
img <- load.image(file = "./shenqinvxia.jpg")
图像即数据
X:图像点(像素)的水平位置。 Y:图像点(像素)的垂直位置。 cc:表示的颜色通道:1(红色)、2(绿色)、3(蓝色) 值:颜色通道的值,从0到1。
# 将图像转换为数据框
img_df <- as.data.frame(img)
head(img_df)## x y cc value
## 1 1 1 1 0.6745098
## 2 2 1 1 0.7529412
## 3 3 1 1 0.7764706
## 4 4 1 1 0.7843137
## 5 5 1 1 0.7372549
## 6 6 1 1 0.6549020library(kableExtra)
library("knitr")
# Show a table of the first 10 rows of the data frame
img_df %>%
arrange(x, y, cc) %>% # sort by columns for viewing
filter(row_number() < 10) %>% # Select top 10 columns
kable("html") %>% # Display table in R Markdown
kable_styling(full_width = F) # Don't take up full width
x | y | cc | value |
---|---|---|---|
1 | 1 | 1 | 0.6745098 |
1 | 1 | 2 | 0.6784314 |
1 | 1 | 3 | 0.6980392 |
1 | 2 | 1 | 0.7137255 |
1 | 2 | 2 | 0.7176471 |
1 | 2 | 3 | 0.7372549 |
1 | 3 | 1 | 0.6745098 |
1 | 3 | 2 | 0.6784314 |
1 | 3 | 3 | 0.6980392 |
ggplot出图需要这样实现
将颜色转化为RGB格式,需要通过函数rgb实现
# Add more expressive labels to the colors
img_df <- img_df %>%
mutate(channel = case_when(
cc == 1 ~ "Red",
cc == 2 ~ "Green",
cc == 3 ~ "Blue"
))
# Reshape the data frame so that each row is a point
img_wide <- img_df %>%
select(x, y, channel, value) %>%
spread(key = channel, value = value) %>%
mutate(
color = rgb(Red, Green, Blue)
)
head(img_wide)## x y Blue Green Red color
## 1 1 1 0.6980392 0.6784314 0.6745098 #ACADB2
## 2 1 2 0.7372549 0.7176471 0.7137255 #B6B7BC
## 3 1 3 0.6980392 0.6784314 0.6745098 #ACADB2
## 4 1 4 0.6588235 0.6392157 0.6352941 #A2A3A8
## 5 1 5 0.7058824 0.6901961 0.6862745 #AFB0B4
## 6 1 6 0.7568627 0.7411765 0.7372549 #BCBDC1
鉴于原图过大,这里过滤
过滤原理是:将图像完整数据框中的点为库,随机抽取部分的点进行出图。sample(nrow(img_wide), sample_size)。
# Take a sample of rows from the data frame
sample_size <- 30000
img_sample <- img_wide[sample(nrow(img_wide), sample_size), ]
# Plot only the sampled points
ggplot(img_sample) +
geom_point(mapping = aes(x = x, y = y, color = color)) +
scale_color_identity() +
scale_y_reverse() +
theme_void()
### 构建随机数字并映射为图形点的大小,达到美术效果# 我们构建随机数字
img_sample$size <- runif(sample_size)
# Plot only the sampled points
ggplot(img_sample) +
geom_point(mapping = aes(x = x, y = y, color = color, size = size)) +
guides(size = FALSE) + # 去除图例
scale_color_identity() +
scale_y_reverse() +
theme_void()
### 使用三原色中的蓝色列为大小,映射到点上# Use the amount of blue present in each point to determine the size
ggplot(img_sample) +
geom_point(mapping = aes(x = x, y = y, color = color, size = Blue)) +
guides(size = FALSE) + # don't show the legend
scale_color_identity() + # use the actual value in the `color` column
scale_y_reverse() + # Orient the image properly (it's upside down!)
theme_void() # Remove axes, background
### 泰森多边形 这种出图我们一般不经常使用,但是此处使用达到某种艺术效果# Create a Voronoi Diagram of the sampled points
ggplot(img_sample) +
geom_voronoi(mapping = aes(x = x, y = y, fill = color)) +
scale_fill_identity() +
scale_y_reverse() +
theme_void()
# 监测图形便
edges <- cannyEdges(img)
# 展示图形轮廓
plot(edges)
将边图像转化为数据框
# 将边图像转化为数据框
edges_df <- edges %>%
as.data.frame() %>%
select(x, y) %>% # only select columns of interest
distinct(x, y) %>% # remove duplicates
mutate(edge = 1) # indicate that these observations represent an edge
在原图形数据框中整合边框数据
# 在原图形数据框中整合边框数据
img_wide <- img_wide %>%
left_join(edges_df)
# Apply a low weight to the non-edge points
img_wide$edge[is.na(img_wide$edge)] <- .05
# Re-sample from the image, applying a higher probability to the edge points
img_edge_sample <- img_wide[sample(nrow(img_wide), sample_size, prob = img_wide$edge), ]
加入边数据,使用维诺图出图,强调图像边
# 重新出图
ggplot(img_edge_sample) +
geom_voronoi(mapping = aes(x = x, y = y, fill = color)) +
scale_fill_identity() +
guides(fill = FALSE) +
scale_y_reverse() +
theme_void() # Remove axes, background
#
ggplot(img_edge_sample) +
geom_point(mapping = aes(x = x, y = y, color = color, size = edge * runif(sample_size))) +
guides(fill = FALSE, size= FALSE) +
scale_color_identity() +
scale_y_reverse() +
theme_void()
原文地址 http://mfviz.com/r-image-art/# Print the image object out
print(img)## Image. Width: 352 pix Height: 220 pix Depth: 1 Colour channels: 3
#可视化图像
plot(img)