Solved

Plot means with confidence intervals by groups in R

Posted on 2008-10-08
5
8,010 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS excel check if Links exist 5 36
Excel - Data Validation 3 29
Clear Filter 8 41
Excel - remove duplicates 1 14
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

823 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