Solved

Plot means with confidence intervals by groups in R

Posted on 2008-10-08
5
7,964 Views
Last Modified: 2013-11-13
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

   abData <- rbind(adf, bdf)
 

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,

      add=TRUE)
 

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

        add=TRUE)

Open in new window

justearth-data-ee.xls
0
Comment
Question by:justearth
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:oheil
ID: 22670771
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 Comment

by:justearth
ID: 22671032
Oli,
Thanks for you suggestion. Please see attached excel spreadsheet above.

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
 
LVL 9

Accepted Solution

by:
oheil earned 500 total points
ID: 22676604
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)

Open in new window

0
 

Author Closing Comment

by:justearth
ID: 31504295
Thanks.
0
 

Author Comment

by:justearth
ID: 22676617
Next time I'll try asking more succinct question.
Cheers,
JE
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now