❝本节来回答VIP会员群中两位观众老爷的问题,「R中计算效应值及如何无缝拼图」,下面通过两个案例来进行展示,结果仅供参考,希望各位观众老爷能够喜欢。 ❞加载R包
library(tidyverse)
library(magrittr)
library(patchwork)
library(aplot)
library(cowplot)
data <- read_tsv("data.txt") %>% pivot_longer(-Sample.taxa) %>%
left_join(.,read_tsv("group.txt") %>%
dplyr::rename(name=sample),by="name") %>%
select(value,group) %>% set_colnames(c("outcome","treatment"))
mean_A = mean(data$outcome[data$treatment == "pre"])
mean_B = mean(data$outcome[data$treatment == "post"])
sd_pooled = sqrt((var(data$outcome[data$treatment == "pre"]) + var(data$outcome[data$treatment == "post"])) / 2)
d <- (mean_A - mean_B) / sd_pooled
# 计算组间平方和(SSB)
SSB <- sum((mean(data$outcome[data$treatment == "pre"]) - mean(data$outcome))^2) +
sum((mean(data$outcome[data$treatment == "post"]) - mean(data$outcome))^2)
# 计算总平方和(SST)
SST <- sum((data$outcome - mean(data$outcome))^2)
# 计算Eta-squared
eta_squared <- SSB / SST
❝R中用于拼图的包有很多,小编常用的主要有「patchwork」,「cowplot」两款,当然「aplot」也属于拼图包的范畴,但是要实现无缝隙的拼图显然「cowplot」更胜一筹。 ❞
p1 <- iris %>%
ggplot(aes(Sepal.Length,Petal.Length,color=Species))+
geom_point()+
theme(legend.position = "non")
p2 <- iris %>%
ggplot(aes(Petal.Width,Petal.Length,color=Species))+
geom_point()+
theme(plot.background = element_blank(),
axis.ticks.y=element_blank(),
axis.text.y = element_blank(),
legend.position = "non")+
labs(y=NULL)
p1+p2+plot_layout(guides = 'collect')
p1 %>% insert_right(p2)
ggdraw()+
draw_plot(p1,scale = 0.9,x=0.02,y=0,width = 0.5,height=1)+
draw_plot(p2,scale = 0.9,x=0.44,y=0,width = 0.5,height=1)