前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >🤒 geomtextpath | 成功让你的ggplot注释拥有傲人曲线!~

🤒 geomtextpath | 成功让你的ggplot注释拥有傲人曲线!~

作者头像
生信漫卷
发布2023-02-24 14:16:15
2920
发布2023-02-24 14:16:15
举报

1写在前面

最近的世界杯结果的确是让人大跌眼镜🕶️, 日本队🇯🇵先后击败世界杯冠军, 德国队🇩🇪和西班牙队🇪🇸, 韩国队🇰🇷逆转葡萄牙🇵🇹, 踩着乌拉圭🇺🇾进入淘汰赛(请韩国队🇰🇷自觉感谢裁判), 让无数人站上天台😂. 不过大家要是看看这几十年日本足球⚽️的发展也就不会觉得奇怪了, 就算有一天日本队将梦想照进现实,捧起大力神杯🏆, 我也不觉得有什么奇怪的. 还是祝各亚洲球队取得好成绩, 也祝梅西C罗在顶峰相遇, 人生不留遗憾😘.


接着是这一期的教程, 最近用了一下geomtextpath, 是个不错的ggplot2扩展包, 让你的geom_text卷起来吧.😂

主要函数有2个, geom_textpathgeom_labelpath, 我们逐一介绍吧.🧐

2用到的包

代码语言:javascript
复制
rm(list = ls())
# remotes::install_github("AllanCameron/geomtextpath", quiet = TRUE)
library(tidyverse)
library(geomtextpath)
library(ggsci)
library(RColorBrewer)

3等价函数

这里我们给大家补充一下在使用text或者label时, 用到的包内对等函数, 不同figure可以选用对应的text或者label.😉

ggplot geom

Text equivalent

Label equivalent

geom_path

geom_textpath

geom_labelpath

geom_segment

geom_textsegment

geom_labelsegment

geom_line

geom_textline

geom_labelline

geom_abline

geom_textabline

geom_labelabline

geom_hline

geom_texthline

geom_labelhline

geom_vline

geom_textvline

geom_labelvline

geom_curve

geom_textcurve

geom_labelcurve

geom_density

geom_textdensity

geom_labeldensity

geom_smooth

geom_textsmooth

geom_labelsmooth

geom_contour

geom_textcontour

geom_labelcontour

geom_density2d

geom_textdensity2d

geom_labeldensity2d

geom_sf

geom_textsf

geom_labelsf

4示例数据

示例数据我们就用大名鼎鼎的Orangeiris吧.

4.1 示例数据一

代码语言:javascript
复制
dat1 <- Orange
DT::datatable(dat1)

4.2 示例数据二

代码语言:javascript
复制
dat2 <- iris
DT::datatable(dat2)

5标注曲线

5.1 简单绘图

这里用到的是geom_textline函数, 一起看一下吧.🥳

代码语言:javascript
复制
dat1 %>% 
dplyr::filter(., Tree == 1) %>% 
  ggplot(aes(x = age, y = circumference)) +
  geom_textline(label = "This is my text oh oh oh!", 
                size = 4, vjust = -0.2,
                linewidth = 1, linecolor = "red4", linetype = 2, 
                color = "deepskyblue4")

5.2 进阶绘图

有时候我们还想标注上复杂的公式, 大家可以这样试一下.😏

代码语言:javascript
复制
lab <- expression(paste("y = ", frac(1, sigma*sqrt(2*pi)), " ",
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)}))

df <- data.frame(x = seq(-2, 0, len = 100),
                 y = dnorm(seq(-2, 0, len = 100)),
                 z = as.character(lab))
df %>% 
ggplot(aes(x, y)) + 
  geom_textpath(aes(label = z), 
                vjust = -0.2, hjust = 0.1, 
                size = 8, parse = T)

6标注densityplot

6.1 绘图

这里用到的是geom_textdensity函数, 我们再改一下颜色主题.🤗

代码语言:javascript
复制
dat2 %>% 
ggplot(aes(x = Sepal.Width, colour = Species, label = Species)) +
  geom_textdensity(size = 6, fontface = 2, hjust = 0.2, vjust = 0.3) +
  scale_color_npg()+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")

6.2 更改文字位置

我们试着把他们改到最大值处.😂

代码语言:javascript
复制
dat2 %>% 
ggplot(aes(x = Sepal.Width, colour = Species, label = Species)) +
  scale_color_npg()+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")+
  
  geom_textdensity(size = 5, fontface = 2, spacing = 50,
                   vjust = -0.2, hjust = "ymax") +
  ylim(c(0, 1.5))

7标注趋势线

这里用到的是geom_labelsmooth函数, method可选如下:👇

  • "lm";
  • "glm";
  • "gam";
  • "loess";

代码语言:javascript
复制
dat2 %>% 
ggplot(aes(x = Sepal.Width, y = Petal.Width, color = Species)) +
  geom_point(alpha = 0.3) +
  geom_labelsmooth(aes(label = Species), 
                   text_smoothing = 30, 
                   fill = "#F6F6FF", # label背景色
                   method = "loess", 
                   formula = y ~ x,
                   size = 4, linewidth = 1, 
                   boxlinewidth = 0.3) +
  scale_color_npg()+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")

Note! 这里你也可以使用geom_textsmooth, 大家自己试一下有什么区别吧.😗

8标注contour lines

我们试着在contour lines上标注一下吧, 用到的函数是eom_textcontour.🤩

代码语言:javascript
复制
df <- expand.grid(x = seq(nrow(volcano)), y = seq(ncol(volcano)))
df$z <- as.vector(volcano)

ggplot(df, aes(x, y, z = z)) + 
  geom_contour_filled(bins = 6, alpha = 0.8) + 
  geom_textcontour(bins = 6, size = 2.5, straight = T) + 
  scale_fill_manual(values = colorRampPalette(brewer.pal(9,"Greens"))(9))+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")

9标注阈值线

我们在这里可以使用geom_texthline, geom_textvline, geom_textabline来进行各种阈值线的绘制.🤓

代码语言:javascript
复制
dat2 %>% 
ggplot(aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  geom_texthline(yintercept = 3, 
                 label = "hline", 
                 hjust = 0.8, color = "red4") +
  geom_textvline(xintercept = 6, 
                 label = "vline", 
                 hjust = 0.8, color = "blue4",
                 linetype = 2, vjust = 1.3) +
  geom_textabline(slope = 15, intercept = -100, 
                  label = "abline", 
                  color = "green4", hjust = 0.6, vjust = -0.2)+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")

10标注统计学差异

我们常规使用直线或者直接标注*的方式来展示统计学差异.😘 这里我们试试换一种方式来绘图吧.🫢

代码语言:javascript
复制
dat2 %>% 
ggplot(aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot(aes(fill = Species))+
  geom_textcurve(data = data.frame(x = 1, xend = 2, 
                                   y = 8.72, yend = 8.52), 
                 aes(x, y, xend = xend, yend = yend), 
                 hjust = 0.35, ncp = 20,
                 curvature = -0.8, 
                 label = "significant difference") +
  scale_y_continuous(limits = c(4, 14))+
  scale_fill_aaas()+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")

11test_smoothing

有的时候画出来的figure过于尖锐, 不够平滑, 这样就不好标注了, 这里也提供了text_smoothing参数用来解决这个问题, 0 (none) to 100 (maximum), 大家试一下吧, 我在这里先做一个示范.😘

代码语言:javascript
复制
dat2 %>% 
ggplot(aes(Sepal.Length, Petal.Length)) +
  geom_textline(linecolour = "red4", size = 4, vjust = -7.5,
                label = "smooth_text", text_smoothing = 40)

12复杂绘图的坐标轴改变

12.1 初步绘图

我们先画个图, 然后我们再把坐标coord_polar().🤩

代码语言:javascript
复制
p <- data.frame(x1 = c(seq(0, 10/6 * pi, pi/3),
                  seq(0, 10/6 * pi, 2*pi/3)),
           y1 = c(rep(2, 6), rep(-1, 3)),
           x2 = c(seq(0, 10/6 * pi, pi/3)  + pi/3,
                  seq(0, 10/6 * pi, 2*pi/3) + 2*pi/3),
           y2 = c(rep(4, 6), rep(2, 3)),
           group = letters[c(1:6, (1:3) * 2)],
           alpha = c(rep(1, 6), rep(0.4, 3))) |>
  ggplot(aes(x1, y1)) +
  geom_rect(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = group,
                alpha = alpha),
            color = "white", size = 2) +
  geom_textpath(data = data.frame(x1 = seq(0, 2 * pi, length = 300),
           y1 = rep(0.5, 300),
           label = rep(c("A and B", "C and D", "E and F"), each = 100)),
           aes(label = label), linetype = 0, size = 8,
           upright = TRUE) +
  geom_textpath(data = data.frame(x1 = seq(0, 2 * pi, length = 300),
           y1 = rep(3, 300),
           label = rep(c("apple", "banana", "cucumber", "durian",
                         "egg", "flower"), 
                       each = 50)),
           aes(label = label), linetype = 0, size = 4.6, color = "white",
           upright = TRUE) +
  scale_y_continuous(limits = c(-5, 4)) +
  scale_x_continuous(limits = c(0, 2*pi)) +
  scale_fill_npg()+
  scale_alpha_identity() +
  theme_void() +
  theme(legend.position = "none") 

p

12.2 更改坐标系

这个包非常强大, 大家完全不用担心使用coord_polar()后, 文字的位置会有改变, 请放心使用!😂

代码语言:javascript
复制
p + coord_polar()

Nice! 这种图无论是在研究型paper还是Review中使用, 都是可以拉高水平的图🌟~


12.3 直接使用coord_curvedpolar()

在这种polar式的坐标系中, 如果标注的文字太长, 我们可以使用coord_curvedpolar(), 要比coord_polar()合适, 大家试一试吧.😉

代码语言:javascript
复制
df <- data.frame(x = c("Apple label", "Banana label",
                       "Cucumber label", "Durian label"),
                 y = c(7, 10, 12, 5))

p <- ggplot(df, aes(x, y, fill = x)) + 
      geom_col(width = 0.5) +
      scale_fill_npg()+
      theme(axis.text.x = element_text(size = 15),
            legend.position = "none")
p

搞定!~🥳🥳🥳

代码语言:javascript
复制
p + coord_curvedpolar()

最后祝大家早日不卷!~


本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信漫卷 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1写在前面
  • 2用到的包
  • 3等价函数
  • 4示例数据
    • 4.1 示例数据一
      • 4.2 示例数据二
      • 5标注曲线
        • 5.1 简单绘图
          • 5.2 进阶绘图
          • 6标注densityplot
            • 6.1 绘图
              • 6.2 更改文字位置
              • 7标注趋势线
              • 8标注contour lines
              • 9标注阈值线
              • 10标注统计学差异
              • 11test_smoothing
              • 12复杂绘图的坐标轴改变
                • 12.1 初步绘图
                  • 12.2 更改坐标系
                    • 12.3 直接使用coord_curvedpolar()
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档