• Status: Solved
• Priority: Medium
• Security: Public
• Views: 8260

Plot means with confidence intervals by groups in R

Hello,
I am trying to plot a graph that uses the mean instead of a density/count on the Y-axis. I of course want to use groups and I would like to also include error bars (standard deviation). I found this code below. My data is in a different format.

Q1: How can I select data that has 'scape' = 2 and 'Pri_No' = 1 or 2.
Q2: Plot this selection for one of the other variable i.e. bbED
So this plot would have the mean bbED from scape=2 and the groups to be plotted would be Pri_No = 1 & 2.

I have also considered using:
bargraph.CI  (http://mutualism.williams.edu/sciplot/)
or barp function in the plotrix package

Thanks,
justearth

``````# ------- bar plot for STD, 95%CI and SEM ------

a <- c(25, 30, 34, 38, 43, 43, 46, 46, 49, 49, 52, 52, 55, 55, 58, 58, 62, 64,
66, 75)
b <- c(5, 5, 35, 44, 44, 47, 51, 55, 55, 58, 58, 62, 64, 64, 67, 67, 71, 74,
79, 85)

## Convert data vectors to dataframes
adf <- data.frame(Group = " Group A ", Measure = a)
bdf <- data.frame(Group = " Group B ", Measure = b)

## Combine into a dataframe using rbind

attach(abData)

tmp = split(Measure, Group)

means = sapply(tmp, mean)
medians = sapply(tmp, median)

stdev = sapply(tmp, sd)

# to calculate SE
require(sciplot)
se = sapply(tmp, se)

n     <- sapply(tmp, length)
ciw   <- qt(0.975, n) * stdev / sqrt(n)

# to make the 3 graphs in one row...
# however they actually don't look nice :-(
# maybe by controlling the "ylim" part things get better
par(mfrow=c(1,3))

# typeface set to Serif (or Times)
par(family="serif")

# ----- bar plot with SD
require(gplots)
plotCI(barcol="blue", pch=16, col="blue", barplot(means, col=c("white",
"gray90"),
ylim=c(0,max(means + stdev)), xlab = "Error bars: ±1SD", ylab="Measure")
, means, stdev,

# --------- bar plot with 95%CI

plotCI(barcol="red", pch=16, col="red", barplot(means, col=c("white", "grey90"),
ylim=c(0,max(means + ciw)), xlab = "Error bars: 95% CI", ylab="Measure"),
means, uiw=ciw,
``````
justearth-data-ee.xls
0
justearth
• 3
• 2
1 Solution

Commented:
You propably got no answer, because your question is not very clear. Please be more specific in the following topics:

1) Give an example of your "data format". Create some fictive data which is in the same format than your data!

2) Your parameters "scape" and "Pri_No" should also appear somewhere in your examples. In your description above you do not gibe any hints, how they are used in your data.

3) After you explain your data format, be as specific as possible how you want to display the data.
Which means over which groups, ans so on.

Regards,

Oli
0

Author Commented:
Oli,

Columns 1,2, 11-16 are ways to group the data or describe the data. The rest of the columns are results from analyses.
Tally=location, scape=treatment, etc.
bbContag, bbED, etc slope, etc are variables with their value recorded.

I want to plot a graph that contains the means/confidence bars for a given variable say 'bbED'. But I just want to use those values where 'scape'=2. I would then like to group this data (and plot) by 'Pri_No'=1,2 (out of 1,2,3,4).
This did not work.

1) Give an example of your "data format". Create some fictive data which is in the same format than your data! My data is attached, should I still create fictive data?

2) Your parameters "scape" and "Pri_No" should also appear somewhere in your examples. In your description above you do not gibe any hints, how they are used in your data. I hope I have now cleared this up, if not please ask more. Also, please see attached data above.

3) After you explain your data format, be as specific as possible how you want to display the data.
Which means over which groups, ans so on. For the barchart: There would be mean for bbED on the Y-axis, groups on the X-axis.

Thanks,
JE

Column names:
bbContag      bbED      bbENN_MN      bbLPI      bbLSI      bbPAFRAC      bbPD      bbPROX_MN      pfor      PriNo      sc_ex      Sc_ex_pri      sc_recov      Pri_No      RecovUnit      slope      std slope      CI SLOPe      plus slope
0

Commented:
Here is my solution (sorry that I missed the attached excel file in first place) asuming that your excel sheet is saved as a tab separated txt file in the current directory as justearth-data-ee.txt:

``````df <- read.csv("justearth-data-ee.txt",sep="\t",dec = ",",header=TRUE,as.is=TRUE)
df\$scape.f <- as.factor( df\$scape  )
df\$Pri_No.f <- as.factor( df\$Pri_No  )
df\$scape.Pri_No.f <- as.factor( paste(df\$scape,df\$Pri_No,sep="_" ) )

df.result <- data.frame( bbED.mean = tapply( df\$bbED, df\$scape.Pri_No.f, function(x){mean(x)} ) )
df.result\$bbED.sd <- tapply( df\$bbED, df\$scape.Pri_No.f, function(x){sd(x)} )

require(sciplot)
df.result\$bbED.se <- tapply( df\$bbED, df\$scape.Pri_No.f, function(x){se(x)} )

df.result\$scape <- tapply( df\$scape, df\$scape.Pri_No.f, function(x){x[1]})
df.result\$Pri_No <- tapply( df\$Pri_No, df\$scape.Pri_No.f, function(x){x[1]})

means <- df.result\$bbED.mean[which(df.result\$scape==2&df.result\$Pri_No<=2)]
stdev <- df.result\$bbED.sd[which(df.result\$scape==2&df.result\$Pri_No<=2)]

xlabel <- c("",rownames(df.result\$bbED.mean[which(df.result\$scape==2&df.result\$Pri_No<=2)]),"")

require(gplots)
plotCI(x=means,uiw=stdev,ylab="mean",xlab="scape_Pri_No",xlim=c(0,3),xaxt="n")
axis(side=1,at=0:3,labels=xlabel)
``````
0

Author Commented:
Thanks.
0

Author Commented:
Next time I'll try asking more succinct question.
Cheers,
JE
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.