Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R堆叠条形图,包括;其他";(使用ggplot2)_R_Plot_Ggplot2 - Fatal编程技术网

R堆叠条形图,包括;其他";(使用ggplot2)

R堆叠条形图,包括;其他";(使用ggplot2),r,plot,ggplot2,R,Plot,Ggplot2,我想制作一个堆积条形图,描述三个不同季节两个地点的分类群的丰度。我用的是ggplot2。绘制图是可以的,但我有48个分类群,所以我在酒吧里有很多不同的颜色。只有八个分类群频繁且丰富地出现,因此我想将其他分类群归为“其他”分类群 我的数据如下所示: SampleID TransectID SampleYear Season Location Taxa1 Taxa2 Taxa3 .... Taxa48 BW15001 1

我想制作一个堆积条形图,描述三个不同季节两个地点的分类群的丰度。我用的是ggplot2。绘制图是可以的,但我有48个分类群,所以我在酒吧里有很多不同的颜色。只有八个分类群频繁且丰富地出现,因此我想将其他分类群归为“其他”分类群

我的数据如下所示:

SampleID     TransectID     SampleYear     Season     Location    Taxa1     Taxa2     Taxa3 .... Taxa48
BW15001              1            2015     fall        SiteA         25         0         0           0
BW15001              2            2015     fall        SiteA         32         0         0           2
BW15001              2            2015     fall        SiteA          6         0        45           0
BW15001              3            2015     fall        SiteA         78         1         2           0   
这是我尝试过的(修改自):

y一种方法:

library(plyr)
d=data.frame(SampleID=rep('BW15001',4),
             TransectID=c(1,2,2,3),
             SampleYear=rep(2015,4),
             Taxa1=c(25,32,6,78),
             Taxa2=c(0,0,0,1),
             Taxa3=c(0,0,45,3))
#Reshape the df so that all taxa columns are melted into two
d=melt(d,id=colnames(d[,1:3]))
d$variable=as.character(d$variable)

# rename all uninteresting taxa as 'other'
`%ni%` <- Negate(`%in%`) # Here I decided to select the ones to keep, but the other way around is fine as well of course
d[d$variable %ni% c('Taxa1','Taxa2'),'variable']='Other' #here you could add a function to automatically determine which taxta you want to keep, as you already did

# aggregate all data for 'other'
d=ddply(d,colnames(d[,1:4]),summarise,value=sum(value)) 

#make your plot, this one is just a bad example
ggplot(d,aes(SampleID,value,fill=variable))+
  geom_bar(stat="identity")+
  facet_grid(. ~ Type, drop=TRUE,scale="free",space="free_x")
库(plyr)
d=数据帧(样本ID=代表('BW15001',4),
横切面=c(1,2,2,3),
样本年份=代表(2015,4),
Taxa1=c(25,32,6,78),
Taxa2=c(0,0,0,1),
Taxa3=c(0,0,45,3))
#重塑df,使所有分类单元列融合为两个
d=熔体(d,id=colnames(d[,1:3]))
d$variable=as.character(d$variable)
#将所有不感兴趣的分类群重命名为“其他”

`%ni%`对我的评论进行了扩展。看看这个包裹。如果没有完整的示例,很难说,但以下内容应该有效:

library(tidyverse)
library(forcats)

temp <- df %>%
  gather(taxa, amount, -c(1:5))

# Reshape the data so that that there is one record per each amount
tidy_df <- temp[rep(rownames(temp), times = temp$amount), ]

tidy_df %>%
  select(-amount) %>%
  mutate(taxa = fct_lump(taxa, n = 2)) %>%       # Check out this line
  ggplot(., aes(x = SampleID, fill = taxa)) +
    geom_bar()

你如何定义主要分类群?总体发生率?发生频率?请查看包装。具体来说,
fct\u lump()
主要分类群占总丰度的90%以上。谢谢。你能解释一下我如何添加一个函数来自动确定要保留哪些分类群吗?例如,占总丰度90%的那些?(或两者兼而有之。)
library(plyr)
d=data.frame(SampleID=rep('BW15001',4),
             TransectID=c(1,2,2,3),
             SampleYear=rep(2015,4),
             Taxa1=c(25,32,6,78),
             Taxa2=c(0,0,0,1),
             Taxa3=c(0,0,45,3))
#Reshape the df so that all taxa columns are melted into two
d=melt(d,id=colnames(d[,1:3]))
d$variable=as.character(d$variable)

# rename all uninteresting taxa as 'other'
`%ni%` <- Negate(`%in%`) # Here I decided to select the ones to keep, but the other way around is fine as well of course
d[d$variable %ni% c('Taxa1','Taxa2'),'variable']='Other' #here you could add a function to automatically determine which taxta you want to keep, as you already did

# aggregate all data for 'other'
d=ddply(d,colnames(d[,1:4]),summarise,value=sum(value)) 

#make your plot, this one is just a bad example
ggplot(d,aes(SampleID,value,fill=variable))+
  geom_bar(stat="identity")+
  facet_grid(. ~ Type, drop=TRUE,scale="free",space="free_x")
library(tidyverse)
library(forcats)

temp <- df %>%
  gather(taxa, amount, -c(1:5))

# Reshape the data so that that there is one record per each amount
tidy_df <- temp[rep(rownames(temp), times = temp$amount), ]

tidy_df %>%
  select(-amount) %>%
  mutate(taxa = fct_lump(taxa, n = 2)) %>%       # Check out this line
  ggplot(., aes(x = SampleID, fill = taxa)) +
    geom_bar()
df %>%
  gather(taxa, amount, -c(1:5)) %>%
  mutate(amount = na_if(amount, 0)) %>%
  na.omit() %>%
  mutate(taxa = fct_lump(taxa, n = 2)) %>%
  ggplot(., aes(x = SampleID, fill = taxa)) +
   geom_bar()