gpt4 book ai didi

r - 按天分组拆分条形图

转载 作者:行者123 更新时间:2023-12-04 11:06:58 25 4
gpt4 key购买 nike

我使用这段代码生成了以下条形图:

MD1<-read.csv("MD_qual_OTU_sorted.csv")

MD1<-data.frame(Samples=c("A","B","C","D","E","F","G","H","I","J","K","L","M", "N","O","P","Q", "R"), Number.of.OTUs=c(13,10,9,9,15,11,7,7,9,9,5,10,10,7,15,17,8,9))
par(las=1)

barplot(MD1[,2],names.arg=MD1[,1], ylab='OTU Count', yaxt='n', xlab='MD samples', main='Total OTU count/Sample',density=c(90,90, 90, 90, 90, 90, 10, 10, 10, 10, 10, 10, 40, 40, 40, 40, 40, 40), col=c("yellow","yellow","pink", "pink","green","green","red","red", "purple", "purple", "blue", "blue", "orange", "orange","cyan", "cyan","chartreuse4", "chartreuse4" ))

enter image description here

usr <- par("usr")
par(usr=c(usr[1:2], 0, 20))
axis(2, at=seq(0,20,5))

我想将 sample A-F 分成一个单独的组(第 3 天)、G-L(第 5 天)和 M-R(第 15 天)

发布了类似的问题,但我不确定如何整理我输入数据的方式才能使用这些解决方案。

最佳答案

您可以考虑使用 ggplot2,使用 facet_wrapfacet_grid 可以很容易地绘制单独的图。

library(ggplot2)


#create a grouping variable
MD1$Day <- rep(c("Day 03","Day 05","Day 15"),
each=6)

p1 <- ggplot(MD1, aes(x=Samples,y=Number.of.OTUs)) +
geom_bar(stat="identity") + facet_wrap(~Day,
scales="free_x")
p1

enter image description here

或者,如果您想使用 base-R 并接近您的原始图像: #添加颜色/密度

MD1$col <-  c("yellow","yellow","pink", "pink","green","green","red","red", 
"purple", "purple", "blue", "blue", "orange", "orange","cyan", "cyan","chartreuse4", "chartreuse4" )
MD1$density <- c(90,90, 90, 90, 90, 90, 10, 10, 10, 10, 10, 10, 40, 40, 40, 40, 40, 40)

#set 1 row three cols for plotting
par(mfrow=c(1,3))
#split and plot
lapply(split(MD1, MD1$Day),function(x){
barplot(x[,2],
names.arg=x[,1],
ylab='OTU Count',
ylim=c(0,20),
main=unique(x$Day),
col=x$col,
density=x$density)
})

enter image description here

关于r - 按天分组拆分条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com