jamesbang

V1

2022/08/29阅读：63主题：雁栖湖

# Part1boxjitter | 完美复刻Nature上的高颜值统计图

## 22. 用到的包

``rm(list = ls())library(tidyverse)library(cowplot)library(ggparl)``

## 33. 示例数据

``dat <- read.csv("geom_boxjitter_example.csv",row.names = 1)``

## 44. 数据整理

@(｡･o･)@

``dat_long <- dat %>%    gather(key, value, 1:6) %>%   mutate(loc = factor(loc, levels = c("abro", "dome")),         type = factor(type),         key = factor(key))``

## 55. 开始画图

#### 5.1 初步绘图

``p1 <- ggplot(dat_long, aes(x = type, y = value, fill = key))+  geom_boxjitter(outlier.color = NA,                  jitter.shape = 21,                  jitter.color = NA,                  jitter.height = 0.05,                  jitter.width = 0.075,                  errorbar.draw = TRUE) p1``

#### 5.2 减少分组

``dat_long <- dat_long %>%   filter(key %in% c("p1", "p2","p3","p4"))  p2 <- ggplot(dat_long,             aes(x = type, y = value, fill = key))+  geom_boxjitter(outlier.color = NA,                  jitter.shape = 21,                  jitter.color = NA,                  jitter.height = 0.05,                  jitter.width = 0.075,                  errorbar.draw = TRUE) p2``

#### 5.3 修改细节

``p3 <- p2 +  theme_bw()+  theme(panel.grid = element_blank(),        panel.border = element_blank(),        axis.line = element_line(colour = "black"),        legend.position = "none") +  ylim(c(-0.1, 1.05)) +   scale_fill_manual(values = c("#0E72BA", "#D95426",                               "#ecb21e", "#812e91"))p3``

#### 5.4 只展示其中比较好看的两组

``p4 <- ggplot(  dat_long %>% filter(key %in% c("p1", "p2")),   aes(x = type, y = value, fill = key)) +  geom_boxjitter(outlier.color = NA, jitter.shape = 21, jitter.color = NA,                  jitter.height = 0.05, jitter.width = 0.075, errorbar.draw = TRUE) +  theme_bw()+  theme(panel.grid = element_blank(),        panel.border = element_blank(),        axis.line = element_line(colour = "black"),        legend.position = "none") +  ylim(c(-0.05, 1.05)) +   scale_fill_manual(values = c("#ecb21e", "#812e91"))p4``

#### 5.5 加上统计值

``library(ggpubr)# 添加p值p5 <- p4 +   stat_compare_means(#label = "p.signif",                      #label.x = 1.5,                      method = "t.test",                     label.y = 1) p5``

## 66. 最后把上面的结果都拼到一起吧

``plot_grid(p1, p2, p3, p5,           labels = "AUTO",          label_size = 30)``

##### jamesbang
V1

wx🔍: Grassssss 卷起来了