Reputation: 33
I'm trying to use ggplot to make a graph that has the composition of substrates at 6 different sites and at 7 different times. The problem is I have different amount of samples for each sampling period and site. I essentially want the code y=freq/(#of stations in that time period)
. The following is a sample of my data set
Substrate Time Site Freq
1 Floc July 11 P1 4
2 Fine July 11 P1 2
3 Medium July 11 P1 12
4 Coarse July 11 P1 0
5 Bedrock July 11 P1 3
6 Floc Aug 11 P1 7
7 Fine Aug 11 P1 1
8 Medium Aug 11 P1 7
9 Coarse Aug 11 P1 1
10 Bedrock Aug 11 P1 4
Therefore I want
Var1 Var2 Var3 Freq
1 Floc July 11 P1 4/(21 - The number of samples taken in July).
Any ideas on how to write this code and then plot the results?
Upvotes: 3
Views: 208
Reputation: 66819
With a data.table (from the package of the same name)...
require(data.table)
DT <- data.table(dat)
DT[,Freq2:=Freq/sum(Freq),by=Var2]
which gives
Var1 Var2 Var3 Freq Freq2
1: Floc July 11 P1 4 0.1904762
2: Fine July 11 P1 2 0.0952381
3: Medium July 11 P1 12 0.5714286
4: Coarse July 11 P1 0 0.0000000
5: Bedrock July 11 P1 3 0.1428571
6: Floc Aug 11 P1 7 0.3500000
7: Fine Aug 11 P1 1 0.0500000
8: Medium Aug 11 P1 7 0.3500000
9: Coarse Aug 11 P1 1 0.0500000
10: Bedrock Aug 11 P1 4 0.2000000
EDIT: The question now has better column names, so it's clearer what "for...period and site" means. As @DWin wrote in the comments, the answer now is:
DT[,Freq2:=Freq/sum(Freq),by='Time,Site']
Upvotes: 5
Reputation: 25726
Have a look at ?ave
:
df <- read.table(textConnection("
Var0 Var1 Var2 Var3 Freq
1 Floc July 11 P1 4
2 Fine July 11 P1 2
3 Medium July 11 P1 12
4 Coarse July 11 P1 0
5 Bedrock July 11 P1 3
6 Floc Aug 11 P1 7
7 Fine Aug 11 P1 1
8 Medium Aug 11 P1 7
9 Coarse Aug 11 P1 1
10 Bedrock Aug 11 P1 4"), header=TRUE, row.names=1)
df$freq <- ave(df$Freq, df$Var1, FUN=function(x)x/sum(x))
df
# Var0 Var1 Var2 Var3 Freq freq
#1 Floc July 11 P1 4 0.1904762
#2 Fine July 11 P1 2 0.0952381
#3 Medium July 11 P1 12 0.5714286
#4 Coarse July 11 P1 0 0.0000000
#5 Bedrock July 11 P1 3 0.1428571
#6 Floc Aug 11 P1 7 0.3500000
#7 Fine Aug 11 P1 1 0.0500000
#8 Medium Aug 11 P1 7 0.3500000
#9 Coarse Aug 11 P1 1 0.0500000
#10 Bedrock Aug 11 P1 4 0.2000000
Upvotes: 3