Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Plot means with confidence intervals by groups in R

Posted on 2008-10-08
5
Medium Priority
?
8,128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

609 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