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 = "./iron_man.jpg")
# Print the image object out
print(img)## Image. Width: 352 pix Height: 220 pix Depth: 1 Colour channels: 3
#可视化图像
plot(img)
### 图像即数据 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.07450980
## 2 2 1 1 0.05490196
## 3 3 1 1 0.04705882
## 4 4 1 1 0.06274510
## 5 5 1 1 0.07843137
## 6 6 1 1 0.07450980library("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.0745098 |
1 | 1 | 2 | 0.2117647 |
1 | 1 | 3 | 0.2274510 |
1 | 2 | 1 | 0.0705882 |
1 | 2 | 2 | 0.2078431 |
1 | 2 | 3 | 0.2235294 |
1 | 3 | 1 | 0.0705882 |
1 | 3 | 2 | 0.2078431 |
1 | 3 | 3 | 0.2235294 |
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.2274510 0.2117647 0.07450980 #13363A
## 2 1 2 0.2235294 0.2078431 0.07058824 #123539
## 3 1 3 0.2235294 0.2078431 0.07058824 #123539
## 4 1 4 0.2235294 0.2078431 0.07058824 #123539
## 5 1 5 0.2313725 0.2156863 0.07843137 #14373B
## 6 1 6 0.2392157 0.2235294 0.08627451 #16393D
ggplot展示图像,好像这张图像有点大,需要几分钟
图片反了
# Plot points at each sampled location
ggplot(img_wide) +
geom_point(mapping = aes(x = x, y = y, color = color)) +
scale_color_identity() #这个参数我们不经常用表示,使用数据框中的颜色值来填充
### 我们将图像正过来 scale_y_reverse函数用于调整图像方向,这在我们一般ggplot出图汇总使用的较少ggplot(img_wide) +
geom_point(mapping = aes(x = x, y = y, color = color)) +
scale_color_identity() +
scale_y_reverse() + #调整图像方向,回正
theme_void() # 去除坐标轴和背景
鉴于原图过大,这里过滤
过滤原理是:将图像完整数据框中的点为库,随机抽取部分的点进行出图。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()