想用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.9725490
library(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.4470588
library("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.1647059
library("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()

(0)

相关推荐

  • R绘图笔记 | 柱状图绘制

    R绘图笔记 | 柱状图绘制

  • ggheatmap复现CNS级美图

    前面我们推送了南方医的一个后起之秀的新R包:快来使用ggheatmap强化你的热图吧!目前已经正式被R语言社区的CRAN接受了,大家可以放心的使用起来! 下面是包作者的实战笔记 前言:自从公开gghe ...

  • 绘图技巧 | 议会(项目)图还不会做?快上车~~

    好奇心Log 今天 以下文章来源于DataCharm ,作者宁海涛 DataCharm定期更新 数据分析.数据可视化(商业.学术图表)教程,同时也会涉及机器学习.深度学习模型的构建及应用.所用工具主要 ...

  • ggplot2绘图学习 调整线的类型

    之前我们学习了ggplot绘制单变量,两个连续变量的图形,两个离散型变量.一个离散型变量,一个连续型变量,包括箱图,点图等等.点击专辑查看更多R语言绘图教程. 线条的类型有 基本线条 library( ...

  • 技术贴 | R语言:ggplot绘图的Y轴截断和拼接

    导读 记录一个产生Y轴截断ggplot绘图的方法.先用coord_cartesian根据Y轴把图截断成上下两份,接着用ggarrange拼接到一起,实现去不要的部分 一.准备依赖包 ggarrange ...

  • ggplot2作图小例子

    ggplot2绘制几个常用图形:直方图,密度曲线图,散点图,箱线图,小提琴图,折线图(重点在小提琴图) ggplot2直方图 library(ggplot2) ggplot(data = diamon ...

  • 快来使用ggheatmap强化你的热图吧!

    创作原因 用法 参数 可视化 综合示例 结语 创作原因 目前最为常见的热图绘制R包,主要包括pheatmap和ComplexHeatmap(仅个人使用习惯).它们强大的功能,基本可以满足所有科研人员的 ...

  • 想要轻松做老板,你就要拥有这七张表!#企...

    想要轻松做老板,你就要拥有这七张表!#企业##管理# 1.工作分析表,划清岗位责权利,避免扯皮推诿. 2.绩效考核表,要什么就考核什么,评估工作干好还是干坏. 3.组织架构表,让员工看到未来的希望,提 ...

  • 挑战:只用一只狗狗,能做多少张PPT?

    你好,我是陈西. 今天要挑战的是,只用一只狗狗,看能做多少张PPT呢? 没看错,就是狗狗.为啥是狗狗呢,因为在网上逛荡的时候,发现了一只非常有个性的狗狗. 于是,灵机一动,目标就定为这只狗狗.拿这只狗 ...

  • 这么风景绝美的油画,真的控制不住想收藏,每一张都可以做壁纸!

    别人画画要3年,这里只要3天 说实话 第一眼见到她笔下的油画风景 我就喜欢到移不开眼 这不 小编马上就来和你们分享了 看这风景油画 非常的"写实" 很舒服的呈现自然的景观 笔触和色 ...

  • 如何用PPT来做一张海报?

    可能大家无法想象PPT居然可以制作海报,具体方法小编分几篇来讲解,下面就来开始吧! 首先我们需要制作一张A4纸大小的海报,具体如何去做呢? 我们先设置一下幻灯片的大小. 打开PPT,双击新建一个空白的 ...

  • 用3D做三张很酷的PPT,尤其是最后一张馋哭了隔壁的设计师

    大家好,我是Jason!大家是否对脑洞大开的PPT技巧感兴趣呢?例如随便操作两下,设计师都会感慨"一个做PPT的怎么这么强."今天咱们就来说几个!给做PPT的正名! 先来个简单的, ...

  • 1天做100张图?无需PPT和PS,用这14个网站,在线就能做!

    作者:小叮 编辑:灿灿 大家好,我是一分钟不学习就坐卧不安,一分钟不作图就胸闷气短的@小叮~ 前天我们团队正在三峡游山玩水,刷微博突然看到热搜:#一天做 100 张设计图# 难道有什么新的神器横空出世 ...

  • 20210127晨思夜想004——500多万字能做多少张摘记卡片?

    最近看到一位名师写的赞美一位名校长的文章,不觉被一组数字打动:该名校长阅读了300万字的专业书籍,写下了20万字的读书笔记.500多万字的文摘卡片.100万字的教学笔记. 不禁让我感慨不已,人家是怎么 ...

  • 以自己长大的县城「梓潼」为题,简单做几张PPT

    梓潼,我长大的地方,名字听着还不错吧 比较简单的分享 题外话,这篇其实我想找我老家"元山"但是吧,手机没有素材,网上也没有. 这就很尴尬,但是不能阻挡我要更文的急切心情.选择我长大 ...

  • 【阅读悦读丨散文】张国英《想给你做顿饭》

    [阅读悦读·散文]张国英<春日遐思> 文/张国英 [作者简介]张国英,甘肃民勤人,教师,常有诗歌.散文发表于报刊和网络平台. [本文由作者授权发布] 奶奶"去"了,三天 ...