对于包含数字和字母的字符串向量,sort()和order()等函数可能出现十位数和个位数排序错误的问题。
这对于绘图X轴的顺序非常重要。
例如:
#假设a是表格中的某列
> a <- c("1A","2A","9A" ,"10A","11A","1B","2B","9B" ,"10B","11B")
> sort(a,decreasing = F) #升序
"10A","11A","1A","2A","9A","10B","11B" ,"1B","2B","9B"
#这里10A和11A在1A前,B也是
#原因读取表格时a是因子,在本身因子水平有个排序
> a
"1A","2A","9A" ,"10A","11A","1B","2B","9B" ,"10B","11B"
Levels: 10A 11A 1A 2A 9A 10B 11B 1B 2B 9B
#因此将因子水平排序跟换则能正常排序(使用rev(),因为该函数不受leves的影响,此外好像与正常排序相反)
> a<- factor(a,levels = rev(a))
"1A","2A","9A" ,"10A","11A","1B","2B","9B" ,"10B","11B"
Levels: 11B 10B 9B 2B 1B 11A 10A 9A 2A 1A
#levels时降序,因此再运用sort或者order时decreasing = F参数会出现相反的结果
#sort和order一样
> sort(yangben$X, decreasing = T)
"1A","2A","9A" ,"10A","11A","1B","2B","9B" ,"10B","11B"
> sort(yangben$X, decreasing = F)
"10B" , "9B" , "2B" , "1B", "11A" ,"10A", "9A" ,"2A" , "1A"
实战
yangben <- read.table("qitizhenghe.csv",sep = ",", header = T)
#对X进行排序按照时期升序因此需要对levels水平排序,否则10排在1前面。
#只能用rev(),因为它不受levels水平影响
yangben$X <- factor(yangben$X, levels = rev(yangben$X))
d <- yangben
d <- melt(d, id = 'X')
group <- c(rep(c(paste0("Ap",1:13)),time=7),rep(c(paste0("Ac",1:13)),time=7),rep(c(paste0("Ab",1:13)),time=7))
e<- as.data.frame(cbind(d,group))
p <-c(rep("Bal",time=13),rep("O",time=13),rep("HS",time=13),
rep("H",time=13),rep("CO2",time=13),rep("CO",time=13),rep("CH",time=13))
e$color <- c(rep(p,time=3))
e <- e[order(e$X,decreasing = T),]
#以group列绘制横坐标(顺序按照group的levels)。因此需要按照想要排列顺序对group的levels进行排序
e$group <- factor(e$group, levels = e[!duplicated(e$group),4])
plot1 = ggplot(e, aes(group , value, fill = color))+
#scale_fill_manual(values = color_class)+
geom_col(position='stack') +
labs(x='Samples', y='Relative Abundance (%)')+
scale_y_continuous(expand=c(0, 0))+
theme(panel.grid = element_blank(), panel.background = element_rect(color = 'black', fill = 'transparent'))+
#theme(axis.line.x = element_line(colour = "white"), axis.text.x = element_blank(),
# axis.ticks.x = element_blank(),
# legend.position = c(0.85,0.6))
theme(axis.text.x=element_text(angle=45))
plot1
最终数据形式
绘图