想用ggplot做一张致谢ppt,但是碰到了520,画风就变了
汇报后我想在最后一张ppt上展示thank you,但是我希望用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
setwd("D:/Shared_Folder/wentao_tech/a2R语言技术/R语言脚本学习合集/R语言图像和绘画等操作")
# Load an image into R
img <- load.image(file = "./thank.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.9803922
## 2 2 1 1 0.9843137
## 3 3 1 1 0.9882353
## 4 4 1 1 0.9921569
## 5 5 1 1 0.9843137
## 6 6 1 1 0.9725490library(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.9803922 |
1 | 1 | 2 | 0.9803922 |
1 | 1 | 3 | 0.9803922 |
1 | 2 | 1 | 0.9882353 |
1 | 2 | 2 | 0.9882353 |
1 | 2 | 3 | 0.9882353 |
1 | 3 | 1 | 0.9921569 |
1 | 3 | 2 | 0.9921569 |
1 | 3 | 3 | 0.9921569 |
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.9803922 0.9803922 0.9803922 #FAFAFA
## 2 1 2 0.9882353 0.9882353 0.9882353 #FCFCFC
## 3 1 3 0.9921569 0.9921569 0.9921569 #FDFDFD
## 4 1 4 0.9921569 0.9921569 0.9921569 #FDFDFD
## 5 1 5 0.9882353 0.9882353 0.9882353 #FCFCFC
## 6 1 6 0.9764706 0.9764706 0.9764706 #F9F9F9
鉴于原图过大,这里过滤节点,清晰度下降
过滤原理是:将图像完整数据框中的点为库,随机抽取部分的点进行出图。sample(nrow(img_wide), sample_size)。
# Take a sample of rows from the data frame
sample_size <- 15000
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()
### 构建随机数字并映射为图形点的大小,达到美术效果
使用三原色中的蓝色列为大小,映射到点上
泰森多边形 这种出图我们一般不经常使用,但是此处使用达到某种艺术效果
# 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()
到这里我觉得可以啦,因此在当我准备收手的时候有个小伙子来啦。
当ggplot遇到520 -------- 画风急转
用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 = "./cutegril.jpg")# 将图像转换为数据框
img_df <- as.data.frame(img)
head(img_df)## x y cc value
## 1 1 1 1 0.4941176
## 2 2 1 1 0.4784314
## 3 3 1 1 0.4941176
## 4 4 1 1 0.4588235
## 5 5 1 1 0.4705882
## 6 6 1 1 0.4470588library("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.4941176 |
1 | 1 | 2 | 0.4901961 |
1 | 1 | 3 | 0.6117647 |
1 | 2 | 1 | 0.4862745 |
1 | 2 | 2 | 0.4901961 |
1 | 2 | 3 | 0.6078431 |
1 | 3 | 1 | 0.4862745 |
1 | 3 | 2 | 0.4901961 |
1 | 3 | 3 | 0.6078431 |
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.6117647 0.4901961 0.4941176 #7E7D9C
## 2 1 2 0.6078431 0.4901961 0.4862745 #7C7D9B
## 3 1 3 0.6078431 0.4901961 0.4862745 #7C7D9B
## 4 1 4 0.5960784 0.4901961 0.4745098 #797D98
## 5 1 5 0.6000000 0.4980392 0.4745098 #797F99
## 6 1 6 0.5960784 0.5058824 0.4745098 #798198
鉴于原图过大,这里过滤
过滤原理是:将图像完整数据框中的点为库,随机抽取部分的点进行出图。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()
# Print the image object out
print(img)## Image. Width: 239 pix Height: 220 pix Depth: 1 Colour channels: 3
#这是原图。可视化图像
plot(img)
我女朋友却喜欢皮卡丘
我们用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 = "./pika.jpg")
# Print the image object out
print(img)## Image. Width: 399 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.1725490
## 2 2 1 1 0.1725490
## 3 3 1 1 0.1686275
## 4 4 1 1 0.1647059
## 5 5 1 1 0.1647059
## 6 6 1 1 0.1647059library("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.1725490 |
1 | 1 | 2 | 0.1686275 |
1 | 1 | 3 | 0.1882353 |
1 | 2 | 1 | 0.1686275 |
1 | 2 | 2 | 0.1647059 |
1 | 2 | 3 | 0.1843137 |
1 | 3 | 1 | 0.1607843 |
1 | 3 | 2 | 0.1568627 |
1 | 3 | 3 | 0.1764706 |
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.1882353 0.1686275 0.1725490 #2C2B30
## 2 1 2 0.1843137 0.1647059 0.1686275 #2B2A2F
## 3 1 3 0.1764706 0.1568627 0.1607843 #29282D
## 4 1 4 0.1725490 0.1529412 0.1568627 #28272C
## 5 1 5 0.1686275 0.1490196 0.1529412 #27262B
## 6 1 6 0.1686275 0.1490196 0.1529412 #27262B
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(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()