查看原文
其他

想用ggplot做一张致谢ppt,但是碰到了520,画风就变了

文涛聊科研 微生信生物 2022-05-08

汇报后我想在最后一张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
xyccvalue
1110.9803922
1120.9803922
1130.9803922
1210.9882353
1220.9882353
1230.9882353
1310.9921569
1320.9921569
1330.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
xyccvalue
1110.4941176
1120.4901961
1130.6117647
1210.4862745
1220.4901961
1230.6078431
1310.4862745
1320.4901961
1330.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
xyccvalue
1110.1725490
1120.1686275
1130.1882353
1210.1686275
1220.1647059
1230.1843137
1310.1607843
1320.1568627
1330.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()


欢迎加入微生信生物讨论群,扫描下方二维码添加小编微信,小编带你入伙啦,大牛如云,让交流变得简单。

历史目录

R语言分析技术

  1. 《扩增子16s核心OTU挑选-基于otu_table的UpSet和韦恩图》

  2. 《分类堆叠柱状图顺序排列及其添加合适条块标签》

  3. 《R语言绘制带有显著性字母标记的柱状图》

  4. 《使用R实现批量方差分析(aov)和多重比较(LSD)》

  5. Rstudio切换挂载R版本及本地安装一些包

  6. 玩转R包

扩增子专题

  1. 《16s分析之Qiime数据整理+基础多样性分析》

  2. 《16s分析之差异展示(热图)》

  3. 迅速提高序列拼接效率,得到后续分析友好型输入,依托qiime

  4. https://mp.weixin.qq.com/s/6zuB9JKYvDtlomtAlxSmGw》

  5. 16s分析之网络分析一(MENA)

  6. 16s分析之进化树+差异分析(一)

  7. 16s分析之进化树+差异分析(二)

  8. Qiime2学习笔记之Qiime2网站示例学习笔记

  9. PCA原理解读

  10. PCA实战

  11. 16s分析之LEfSe分析

基于phyloseq的微生物群落分析

  1. 开年工作第一天phyloseq介绍

  2. phyloseq入门

代谢组专题

  1. 非靶向代谢组学数据分析连载(第零篇引子)

  2. 非靶向代谢组学分析连载(第一篇:缺失数据处理、归一化、标准化)

当科研遇见python

1.当科研遇见python

科学知识图谱

杂谈

  1. 我的生物信息之路




您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存