ggplot2绘制方差分析条图叠加散点图

2023-11-13 10:42:13 浏览数 (1)

绘制拟合曲线散点图

代码语言:javascript复制
p2 <- gapminder %>% select(2,1,year,lifeExp,gdpPercap) %>% 
  filter(continent=="Asia") %>% 
  filter(country %in% c("India","Cambodia","Afghanistan","Bangladesh","Indonesia")) %>% 
  ggplot(aes(lifeExp,gdpPercap)) 
  geom_point(aes(color=country)) 
  stat_smooth(linetype=2,color="red",size=0.5) 
  stat_poly_eq(use_label(c("eq","adj.R2","p"), sep = "*"; "*"),
               size=3,label.x.npc = "left", label.y.npc = "top",rr.digits =3) 
  scale_color_manual(values=wes_palette("Zissou1")) 
  scale_y_continuous(position = "right") 
  labs(x=NULL,y=NULL) 
  theme_test() 
  theme(legend.position = "non")

数据清洗

代码语言:javascript复制
df <- read_tsv("data.xls") %>%
  arrange(conc) %>%
  unite("Type", Treatment:Type, sep="_", remove = TRUE, na.rm = FALSE) %>%
  mutate(Plant = str_remove(Plant, "[0-9] ")) %>%
  filter(conc %in% c(95,175,250,350)) %>% 
  split(.$conc)
代码语言:javascript复制
aov_data <- data.frame()

定义函数

代码语言:javascript复制
perform_anova <- function(data) {
  data <- rename(data, Plant=Plant, Type=Type, conc=conc, uptake=uptake)
  
  anova <- aov(uptake ~ Type, data = data)
  Tukey <- TukeyHSD(anova)
  cld <- multcompLetters4(anova, Tukey)
  
  dt <- data %>%
    group_by(Plant, Type, conc) %>%
    summarise(value_mean = mean(uptake), sd = sd(uptake), .groups = 'drop') %>%
    arrange(desc(value_mean))
  
  cld <- as.data.frame.list(cld$`Type`)
  dt$Tukey <- cld$Letters
  dt
}

方差分析

代码语言:javascript复制
aov_data <- bind_rows(lapply(df, perform_anova))

aov_data <- aov_data %>%
  arrange(Plant) %>%
  mutate(conc = fct_relevel(factor(conc), c("95", "175", "250", "350")))

绘制方差分析图

代码语言:javascript复制
p1 <- aov_data %>% 
  ggplot(aes(conc,value_mean,fill=Type)) 
  geom_bar(stat = "identity", position = "dodge",width=0.5)  
  geom_errorbar(aes(ymax = value_mean   sd, ymin = value_mean - sd),
                position = position_dodge(0.5),width = 0.2,color = "Gray25") 
  scale_fill_manual(values=wes_palette("Zissou1")) 
  scale_y_continuous(expand = expansion(0)) 
  geom_text(aes(label=Tukey, y = value_mean   sd   1.5), color = NA,
            show.legend = FALSE,position = position_dodge(0.5)) 
  geom_text(aes(label=Tukey, y = value_mean   sd  0.8), size = 3, color = "black",
            show.legend = FALSE,position = position_dodge(0.5)) 
  labs(x=NULL,y=NULL) 
  theme(axis.ticks.x=element_blank(),
        axis.text.y=element_text(color="black",size = 10,margin = margin(r =2)),
        axis.text.x=element_text(color="black",size = 10),
        panel.background = element_rect(fill = NA,color = NA),
        panel.grid.minor= element_line(size=0.2,color="#e5e5e5"),
        panel.grid.major = element_line(size=0.2,color="#e5e5e5"),
        panel.border = element_rect(fill=NA,color="black",size=0.3,linetype="solid"),
        legend.title = element_blank(),
        legend.text = element_text(color="black",size=8),
        legend.spacing.x=unit(0.1,'cm'),
        legend.key.width=unit(0.4,'cm'),
        legend.key.height=unit(0.4,'cm'),
        legend.position = c(0.35,1),legend.justification=c(1,1),
        legend.background=element_blank())

拼图

代码语言:javascript复制
p1 p2 plot_layout(widths = c(1.5,1))

0 人点赞