Label stacked bar chart with variable other than plotted Y

Jared

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.

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.

#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.

enter image description here

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.

*Update

My actual data looks like this:

actual data image

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.

Adam Kimberley

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:

# 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.

See the geom_text help page for more info.

Hope that edit works with your data.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Stacked Bar Chart Labels - D3

분류에서Dev

Stacked Bar Chart D3js

분류에서Dev

stacked bar and line chart in same graph with any android library?

분류에서Dev

How to change section color of a stacked bar chart in Google Charts API?

분류에서Dev

Horizontal bar chart y axis alignment

분류에서Dev

Highcharts - Detail chart with stacked columns

분류에서Dev

What are some other uses of class methods in Ruby, other than to just set an attribute/variable of a class?

분류에서Dev

vertical bar chart with validation

분류에서Dev

AndroidPlot - Stacked Bar Z-order

분류에서Dev

chart.js label with if statement

분류에서Dev

What are the variable names that are disallowed in cdecl other than the ones that cannot be used in any program?

분류에서Dev

How do I change where the secondary axis data is plotted in Excel chart?

분류에서Dev

Flot Bar Chart Data Conversion

분류에서Dev

Subset ordinal domain for a bar chart

분류에서Dev

Flot Chart's Bar Width

분류에서Dev

Custom bar chart text with Highcharts

분류에서Dev

Stata - Preserving encoded variable and stacked graphing

분류에서Dev

Why do Chart Stacked Columns show up as thin lines?

분류에서Dev

How do I display multiple stacked columns for each date range in an stacked Bar

분류에서Dev

Pandas stacked bar creating many individual plots with incorrect bottom values

분류에서Dev

Google chart vertical axis strange label

분류에서Dev

Excel Chart, Axis Label decimal removal

분류에서Dev

Responding to URL other than Google

분류에서Dev

How to create a bar chart/histogram with bar per discrete value?

분류에서Dev

Dimple.js - Add data labels to each bar of the bar chart

분류에서Dev

Customize chart tooltip as label with line pointing to pie segment

분류에서Dev

Why does stacked bar plot change when add facet in r ggplot2

분류에서Dev

Excel Graph: how can I show two values in the same bar? Not using stacked?

분류에서Dev

How to disable scrolling (other than overflow:hidden)

Related 관련 기사

  1. 1

    Stacked Bar Chart Labels - D3

  2. 2

    Stacked Bar Chart D3js

  3. 3

    stacked bar and line chart in same graph with any android library?

  4. 4

    How to change section color of a stacked bar chart in Google Charts API?

  5. 5

    Horizontal bar chart y axis alignment

  6. 6

    Highcharts - Detail chart with stacked columns

  7. 7

    What are some other uses of class methods in Ruby, other than to just set an attribute/variable of a class?

  8. 8

    vertical bar chart with validation

  9. 9

    AndroidPlot - Stacked Bar Z-order

  10. 10

    chart.js label with if statement

  11. 11

    What are the variable names that are disallowed in cdecl other than the ones that cannot be used in any program?

  12. 12

    How do I change where the secondary axis data is plotted in Excel chart?

  13. 13

    Flot Bar Chart Data Conversion

  14. 14

    Subset ordinal domain for a bar chart

  15. 15

    Flot Chart's Bar Width

  16. 16

    Custom bar chart text with Highcharts

  17. 17

    Stata - Preserving encoded variable and stacked graphing

  18. 18

    Why do Chart Stacked Columns show up as thin lines?

  19. 19

    How do I display multiple stacked columns for each date range in an stacked Bar

  20. 20

    Pandas stacked bar creating many individual plots with incorrect bottom values

  21. 21

    Google chart vertical axis strange label

  22. 22

    Excel Chart, Axis Label decimal removal

  23. 23

    Responding to URL other than Google

  24. 24

    How to create a bar chart/histogram with bar per discrete value?

  25. 25

    Dimple.js - Add data labels to each bar of the bar chart

  26. 26

    Customize chart tooltip as label with line pointing to pie segment

  27. 27

    Why does stacked bar plot change when add facet in r ggplot2

  28. 28

    Excel Graph: how can I show two values in the same bar? Not using stacked?

  29. 29

    How to disable scrolling (other than overflow:hidden)

뜨겁다태그

보관