jamesbang

V1

2022/12/03阅读：74主题：雁栖湖

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

## 2用到的包

``rm(list = ls())# remotes::install_github("AllanCameron/geomtextpath", quiet = TRUE)library(tidyverse)library(geomtextpath)library(ggsci)library(RColorBrewer)``

## 3等价函数

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示例数据

### 4.1 示例数据一

``dat1 <- OrangeDT::datatable(dat1)``

### 4.2 示例数据二

``dat2 <- irisDT::datatable(dat2)``

## 5标注曲线

### 5.1 简单绘图

``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 进阶绘图

``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 绘图

``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 更改文字位置

``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标注趋势线

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

``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

``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标注阈值线

``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标注统计学差异

``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

``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 初步绘图

``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 更改坐标系

``p + coord_polar()``

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

### 12.3 直接使用coord_curvedpolar()

``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``

``p + coord_curvedpolar()``

📍 往期精彩

##### jamesbang
V1

wx🔍: Grassssss 卷起来了