简体   繁体   中英

Plotting confidence intervals data with R

I would like to plot mean CPUE by year and add in CIs that are already calculated.

The CIs are calculated following an approach for trawl survey data so I do not think I can use any of the CI plot functions available in R. I would really appreciate any help.

I have been trying to figure this out following examples I found online, but the CIs are not being plotted on my data points. I have R version 3.1.0 on windows 8. This is my code.

dput(fall)

structure(list(Year = structure(1:7, .Label = c("2007", "2008", 
"2009", "2010", "2011", "2012", "2013", "2014"), class = "factor"), 
    Season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Fall", 
    "Spring"), class = "factor"), CPUE = c(2.67597320766895, 
    1.13720423803133, 3.33880765324431, 0.806172684858967, 1.4489759307485, 
    10.5492990950043, 4.52479039663784), Variance = c(6.80824504958873, 
    0.320707030421567, 11.5769406857122, 1.05791053306542, 0.187046436602381, 
    15.8421823978692, 2.68384838783695), SD = c(2.60926139924476, 
    0.566310012644635, 3.40249036526369, 1.0285477786984, 0.432488654882855, 
    3.98022391303168, 1.63824552123207), Number = c(75, 91, 87, 
    85, 115, 157, 208), CV = c(0.975070076100538, 0.497984437364567, 
    1.01907348929115, 1.27584052153583, 0.298478839920718, 0.377297475139038, 
    0.362059980159386), lower = c(2.07563668109912, 1.01926446969017, 
    2.61363856286983, 0.584320068914576, 1.36908295705723, 9.92183627713643, 
    4.30084507885747), upper = c(3.27630973423878, 1.2551440063725, 
    4.06397674361879, 1.02802530080336, 1.52886890443977, 11.1767619128722, 
    4.7487357144182)), .Names = c("Year", "Season", "CPUE", "Variance", 
"SD", "Number", "CV", "lower", "upper"), row.names = c(NA, 7L
), class = "data.frame")

My plot attempt was:

plot(fall$CPUE, type='n', xlab="Year", ylab='Mean CPUE', axes=F)
axis(1, at=1:8, labels=levels(fall$Year))
axis(2)
box()
lines(fall$Year, fall$CPUE, col=1)
points(fall$Year, fall$CPUE, col=1, pch=16)
arrows(y0 = fall$lower, y1 = fall$upper, x0 = fall$CPUE, x1 = fall$CPUE, 
length=0.1, code = 3, col = 4, angle = 90) 

Andre's answer here helped me to find a solution.

Data for plot

structure(list(CPUE = c(2.67597320766895, 1.13720423803133, 3.33880765324431, 0.806172684858967, 1.4489759307485, 10.5492990950043, 4.52479039663784 ), lower = c(2.25499288520513, 1.04583532734779, 2.80755247046762, 0.640225963050555, 1.37919786502951, 9.90712658332295, 4.26047455308042 ), upper = c(3.09695353013276, 1.22857314871488, 3.870062836021, 0.972119406667378, 1.51875399646749, 11.1914716066857, 4.78910624019525 )), .Names = c("CPUE", "lower", "upper"), class = "data.frame", row.names = c(NA, 7L))

Plot

plot(fall$CPUE, type='l', xlab="Year", ylab='Mean CPUE', axes=F,ylim=c(0,12)) 
axis(1, at=1:8, labels=levels(fall$Year)) 
axis(2) 
box() 

require(plotrix)

plotCI(fall$CPUE,y=NULL,uiw = fall$upper-fall$CPUE,ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),add=T,scol="black",pch=18,pt.bg=par("bg"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM