标签堆积条形图与变量而不是绘制Y.

时间:2022-08-24 14:52:19

I'm working on some fish electroshocking data and looking at fish species abundance per transects in a river. Essentially, I have an abundance of different species per transect that I'm plotting in a stacked bar chart. But, what I would like to do is label the top of the bar, or underneath the x-axis tick mark with N = Total Preds for that particular transect. The abundance being plotted is the number of that particular species divided by the total number of fish (preds) that were caught at that transect. I am having trouble figuring out a way to do this since I don't want to label the plot with the actual y-value that is being plotted.

我正在研究一些鱼类电击数据,并研究河流中每个横断面的鱼类丰度。基本上,我在每个横断面上都有大量不同的物种,我正在叠加的条形图中绘制。但是,我想要做的是标记条形图的顶部,或者在x轴刻度标记下面标记该特定横断面的N = Total Preds。绘制的丰度是特定物种的数量除以在该横断面捕获的鱼类(preds)的总数。我无法找到一种方法来做到这一点,因为我不想用正在绘制的实际y值标记图。

Excuse the crude code. I am newer to R and not super familiar with generating random datasets. The following is what I came up with. Obviously in my real data the abundance % per transect always adds up to 100 %, but the idea is to be able to label the graph with TotalPreds for a transect.

请原谅粗略的代码。我是R的新手,并不熟悉生成随机数据集。以下是我提出的建议。显然,在我的真实数据中,每个横断面的丰度百分比总是高达100%,但我们的想法是能够用横断面的TotalPred标记图形。

#random data
Transect<-c(1:20)
Habitat<-c("Sand","Gravel")
Species<-c("Smallmouth","Darter","Rock Bass","Chub")
Abund<-runif(20,0.0,100.0)
TotalPreds<-sample(1:139,20,replace=TRUE)
data<-data.frame(Transect,Habitat,Species,Abund,TotalPreds)

#Generate plot
AbundChart<-ggplot(data=data,aes(x=Transect,y=Abund,fill=Species))
AbundChart+labs(title="Shocking Fish Abundance")+theme_bw()+
scale_y_continuous("Relative Abundance (%)",expand=c(0.02,0),
  breaks=seq(0,100,by=20),labels=seq(0,100,by=20))+
scale_x_discrete("Transect",expand=c(0.03,0))+
theme(plot.title=element_text(face='bold',vjust=2,size=25))+
theme(legend.title=element_text(vjust=5,size=15))+
geom_bar(stat="identity",colour="black")+
facet_grid(~Habitat,labeller=label_both,scales="free_x")

I get this plot that I would like to label with TotalPreds as described previously.

如前所述,我想用TotalPreds标记这个情节。

标签堆积条形图与变量而不是绘制Y.

Again my plot would have bars that reached 100% for abundance, and in my real data transects 1-10 are gravel and 11-20 are sand. Excuse my poor sample dataset.

再次,我的情节将有足够的条形达到100%,在我的真实数据中,横断面1-10是砾石,11-20是沙。请原谅我糟糕的样本数据集。

*Update

*更新

My actual data looks like this:

我的实际数据如下所示:

标签堆积条形图与变量而不是绘制Y.

Variable in this case is the fish species and value is the abundance of that species at that particular electroshocking transect. Total_Preds is repeated when the data moves to a new species, because total preds is indicative of the total preds caught at that particular transect (i.e. each transect only has 1 total preds value). Maybe the melt function wasn't the right way to analyze this, but I have like 17 fish species that were caught at different rates across these 20 transects. I guess habitat type is singular to a transect as well, with 1-10 being gravel and 11-20 being sand, and that is repeated in my dataset across fish species as well.

在这种情况下,变量是鱼类物种,价值是该物种在该特定电休克断面上的丰度。当数据移动到新物种时,重复Total_Preds,因为总preds指示在该特定样带处捕获的总preds(即每个样带仅具有1个总preds值)。也许融化功能不是分析这种情况的正确方法,但我喜欢在这20个横断面上以不同速率捕获的17种鱼类。我猜栖息地类型对于横断面来说也是单数的,其中1-10是砾石,11-20是沙子,并且在我的数据集中也反复出现。

1 个解决方案

#1


1  

Edited in response to the update, you should be able to create a new dataframe containing the TotalPred data (not repeated) and use that in geom_text. Can't test this without data but maybe:

为响应更新而编辑,您应该能够创建包含TotalPred数据(不重复)的新数据框,并在geom_text中使用它。无法在没有数据的情况下测试,但可能:

# select non-repeated half of melted data for use in geom_text
textlabels <- data[c(1:19),]

#Generate plot
AbundChart<-ggplot(data=data,aes(x=Transect,y=Abund,fill=Species))

AbundChart+labs(title="Shocking Fish Abundance")+theme_bw()+
  scale_y_continuous("Relative Abundance (%)",expand=c(0.02,0),breaks=seq(0,100,by=20),labels=seq(0,100,by=20))+
  scale_x_discrete("Transect",expand=c(0.03,0))+
  theme(plot.title=element_text(face='bold',vjust=2,size=25))+
  theme(legend.title=element_text(vjust=5,size=15))+
  geom_bar(stat="identity",colour="black")+
  facet_grid(~Habitat,labeller=label_both,scales="free_x") + 
  geom_text(data = textlabels, aes(x = Transect_ID, y = value, vjust = -0.5,label = TotalPreds))

You might have to play around with different values for vjust to get the labels where you want them.

您可能需要使用不同的vjust值来获取所需的标签。

See the geom_text help page for more info.

有关详细信息,请参阅geom_text帮助页面。

Hope that edit works with your data.

希望编辑适用于您的数据。

#1


1  

Edited in response to the update, you should be able to create a new dataframe containing the TotalPred data (not repeated) and use that in geom_text. Can't test this without data but maybe:

为响应更新而编辑,您应该能够创建包含TotalPred数据(不重复)的新数据框,并在geom_text中使用它。无法在没有数据的情况下测试,但可能:

# select non-repeated half of melted data for use in geom_text
textlabels <- data[c(1:19),]

#Generate plot
AbundChart<-ggplot(data=data,aes(x=Transect,y=Abund,fill=Species))

AbundChart+labs(title="Shocking Fish Abundance")+theme_bw()+
  scale_y_continuous("Relative Abundance (%)",expand=c(0.02,0),breaks=seq(0,100,by=20),labels=seq(0,100,by=20))+
  scale_x_discrete("Transect",expand=c(0.03,0))+
  theme(plot.title=element_text(face='bold',vjust=2,size=25))+
  theme(legend.title=element_text(vjust=5,size=15))+
  geom_bar(stat="identity",colour="black")+
  facet_grid(~Habitat,labeller=label_both,scales="free_x") + 
  geom_text(data = textlabels, aes(x = Transect_ID, y = value, vjust = -0.5,label = TotalPreds))

You might have to play around with different values for vjust to get the labels where you want them.

您可能需要使用不同的vjust值来获取所需的标签。

See the geom_text help page for more info.

有关详细信息,请参阅geom_text帮助页面。

Hope that edit works with your data.

希望编辑适用于您的数据。