在模仿中精进数据可视化_使用R语言绘制简单图形(以后有大用)
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天继续使用
ggplot2
绘制一些简单的图形,以备将来使用。(我从来不写无意义的推文和代码,各位大佬如果猜到我画这些图的目的,可以评论区进行评论。)
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(ggplot2)
library(patchwork)
library(ggfun)
library(showtext)
font_families()
showtext_auto()
加载数据以及绘图
####----load Data----####
#####----五角星-----#####
fivestar <- seq(pi/2, pi/2 + 2*pi*(1- 1/5), length = 5)
five_star <- data.frame(
x = 3 * cos(fivestar),
y = 3 * sin(fivestar),
id = 1:5
)
p_fivestar <- ggplot(data = five_star, aes(x = x, y = y)) +
geom_polygon(fill = "#a6bddb", color = "black") +
# geom_text(aes(label = id)) +
coord_fixed() +
labs(title = "五角星") +
theme_nothing() +
theme(plot.title = element_text(hjust = 0.5, size = 15))
#####----爱-----#####
heart <- seq(0, 2*pi, by = 0.08)
heat_poly <- data.frame(x=16 * sin(heart)^3,
y=13 * cos(heart) - 5 * cos(2*heart) - 2 * cos(3*heart) - cos(4*heart) + 2
)
p_heart <- ggplot(data = heat_poly, aes(x = x, y = y)) +
geom_polygon(fill = "#fc9272", color = "black") +
coord_fixed() +
labs(title = "爱") +
theme_nothing() +
theme(plot.title = element_text(hjust = 0.5, size = 15))
#####----三角形-----#####
triangle <- seq(pi/2, pi/2 + 2*pi*(1-1/3), length = 3)
trianglepoly <- data.frame(
x = 3 * cos(triangle),
y = 3 * sin(triangle),
id = 1:3
)
p_triangle <- ggplot(data = trianglepoly, aes(x = x, y = y)) +
geom_polygon(fill = "#a6bddb", color = "black") +
# geom_text(aes(label = id)) +
coord_fixed() +
labs(title = "三角形") +
theme_nothing() +
theme(plot.title = element_text(hjust = 0.5, size = 15))
#####----正方形-----#####
square_angle <- seq(0, 2*pi, length.out = 5) # 5个点,包括最后一个点与第一个点相同
square <- data.frame(
x = 3 * cos(square_angle),
y = 3 * sin(square_angle),
id = 1:5
)
p_square <- ggplot(data = square, aes(x = x, y = y)) +
geom_polygon(fill = "#a6bddb", color = "black") +
# geom_text(aes(label = id)) +
coord_fixed() +
labs(title = "正方形") +
theme_nothing() +
theme(plot.title = element_text(hjust = 0.5, size = 15))
p_square
#####----正方形2-----#####
square2 <- data.frame(
x = c(2, 2, -2, -2), # 四个顶点的x坐标
y = c(0, 4, 4, 0), # 四个顶点的y坐标
id = 1:4
)
p_square2 <- ggplot(data = square2, aes(x = x, y = y)) +
geom_polygon(fill = "#a6bddb", color = "black") +
# geom_text(aes(label = id)) +
coord_fixed() +
labs(title = "正方形") +
theme_nothing() +
theme(plot.title = element_text(hjust = 0.5, size = 15))
p_square2
#####----六芒星-----#####
angle <- seq(0, 2*pi, length.out = 7) # 7个点,第一个点和最后一个点重合
sixstar <- data.frame(
x = 3 * cos(angle), # 计算顶点的x坐标
y = 3 * sin(angle), # 计算顶点的y坐标
id = 1:7
)
p_sixstar <- ggplot() +
geom_polygon(data = sixstar, aes(x = x, y = y), fill = "#a6bddb", color = "black") +
coord_fixed() +
labs(title = "六芒星") +
theme_nothing() +
theme(plot.title = element_text(hjust = 0.5, size = 15))
####----combine----####
p_combine <- p_fivestar + p_heart + p_triangle + p_square + p_square2 + p_sixstar
ggsave(filename = "P.pdf",
plot = p_combine,
height = 8,
width = 18)
版本信息
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 15.1.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] showtext_0.9-6 showtextdb_3.0 sysfonts_0.8.8 ggfun_0.1.5 patchwork_1.2.0.9000 ggplot2_3.5.1
loaded via a namespace (and not attached):
[1] utf8_1.2.4 R6_2.5.1 tidyselect_1.2.1 magrittr_2.0.3 gtable_0.3.5 glue_1.8.0 tibble_3.2.1
[8] pkgconfig_2.0.3 generics_0.1.3 dplyr_1.1.4 lifecycle_1.0.4 cli_3.6.3 fansi_1.0.6 scales_1.3.0
[15] grid_4.3.0 vctrs_0.6.5 withr_3.0.1 compiler_4.3.0 rstudioapi_0.15.0 tools_4.3.0 munsell_0.5.1
[22] pillar_1.9.0 colorspace_2.1-1 rlang_1.1.4
历史绘图合集
公众号推文一览
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。