在xyplot点阵中添加置信区间线

时间:2014-07-26 17:26:58

标签: r lattice

我想在格子的xyplot中用置信区间绘制月平均数据。

我的数据

structure(list(Year = c(2002, 2003, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2011, 2013, 2014, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2010, 2011, 2012, 2013, 2014, 2002, 2003, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2002, 2003, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013), Month = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 
9, 9, 9, 9, 9, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11
), Mean_cpue = c(0.029887285368166, 0, 0, 0, 0, 0, 0, 0.0120702974121282, 
0, 0, 0, 0, 0.0885115348151269, 0.0213480609772614, 0, 0, 0.0336124687779683, 
0.0902387724288072, 0.193967094662344, 0.117053091751817, 0.0711632938110342, 
0.0525878717651602, 0, 0.0120702974121282, 0.207579300856452, 
0.0358039256996383, 0.0166040474267589, 0.0121087272883333, 0.0162140524597072, 
0.154270398071093, 0.0120702974121282, 0.150970130037337, 0.12105145401785, 
0.0632248323325959, 0.044830928052249, 0.0121087272883333, 0.108694738590451, 
0.170388057931511, 0.0291672811545349, 0.022782757776964, 0.207999461591582, 
0.0434586888584571, 0.104343852362392, 0.153832063391739, 0.161506724252958, 
0.0653781733909196, 0, 0, 0.237175596387648, 0.121330336973442, 
0.108501000737385, 0.131675971768672, 0.0203079462059764, 0.15906194037712, 
0.079356434733028, 0.121289371115559, 0, 0.107329084588644, 0.0264229783349135, 
0.0120702974121282), SD = c(0.0307696416725129, 0, 0, 0, 0, 0, 
0, 0.00987635581841447, 0, 0, 0, 0, 0.0486162766261555, 0.0217368441347827, 
0, 0, 0.0269145466830504, 0.054756566623845, 0.105225040159409, 
0.0721525938367538, 0.038122773432025, 0.0338730902158114, 0, 
0.00987635581841447, 0.132514957927259, 0.0233750161456372, 0.0154447857892656, 
0.0128724996247272, 0.0308405636799886, 0.134167866465197, 0.0115260638274367, 
0.0703509615907247, 0.0540433073905704, 0.0405719243564806, 0.0359312003314195, 
0.0129682072594002, 0.0899110382201282, 0.0502463758873788, 0.0158505073235041, 
0.0142578023128618, 0.130901473098702, 0.0597329059327505, 0.0876664891263196, 
0.101665634547565, 0.132213481293559, 0.0332650276803448, 0, 
0, 0.534264140740731, 0.114346946756607, 0.0945866368801252, 
0.114814485605315, 0.0590103628489748, 0.174112345497419, 0.0564149319195301, 
0.1126279135683, 0, 0.0770688356576073, 0.0420101392878645, 0.00995089541035718
), CV = c(1.02952279852377, 0, 0, 0, 0, 0, 0, 0.818236326844002, 
0, 0, 0, 0, 0.549264869575471, 1.01821163795323, 0, 0, 0.800731028144289, 
0.606796448467254, 0.542489128594642, 0.616409124756277, 0.535708388277467, 
0.644123617838676, 0, 0.818236326844002, 0.638382330899635, 0.652861821402824, 
0.93018198468736, 1.06307618614301, 1.9020885590835, 0.869692877847946, 
0.954911335975476, 0.46599258789355, 0.446449056139393, 0.641708690393213, 
0.801482411640974, 1.07098020713498, 0.827188504117962, 0.294893764841054, 
0.543434516214402, 0.625815472053082, 0.629335634318774, 1.37447556522696, 
0.840169182386028, 0.660887153861219, 0.818625242417035, 0.508809377121035, 
0, 0, 2.25261008669506, 0.942443164743179, 0.871758197964106, 
0.871947129481008, 2.90577699243702, 1.09461977569629, 0.710905575701857, 
0.928588486628339, 0, 0.718061054494091, 1.58990931133434, 0.824411782957272
), Number = c(41L, 59L, 76L, 69L, 69L, 64L, 68L, 68L, 67L, 68L, 
68L, 67L, 69L, 39L, 76L, 69L, 102L, 65L, 87L, 67L, 68L, 68L, 
68L, 68L, 66L, 61L, 50L, 69L, 39L, 66L, 48L, 69L, 58L, 68L, 67L, 
68L, 69L, 99L, 88L, 74L, 69L, 23L, 76L, 68L, 55L, 67L, 68L, 68L, 
53L, 32L, 52L, 60L, 45L, 42L, 60L, 65L, 67L, 67L, 61L, 67L), 
    lower = c(0.0201751880783295, 0, 0, 0, 0, 0, 0, 0.00967971001140787, 
    0, 0, 0, 0, 0.0768326299832411, 0.0143017912395284, 0, 0, 
    0.0283259501801248, 0.0766707595559661, 0.171540595696753, 
    0.0994536912611494, 0.0619356167687396, 0.0443888372040115, 
    0, 0.00967971001140787, 0.175003058263301, 0.0298173060874761, 
    0.0122146878716445, 0.00901641523774747, 0.00621669823452787, 
    0.121287819843396, 0.00872347735001416, 0.134069983784514, 
    0.106841490715223, 0.0534043344715587, 0.0360666196453495, 
    0.00896975240182204, 0.0870957488779401, 0.160366594952495, 
    0.0258088795504124, 0.0194794928864924, 0.176553494257858, 
    0.0176282433393854, 0.0843112014470561, 0.12922373725362, 
    0.125764413589897, 0.0572641955926423, 0, 0, 0.0899141611372015, 
    0.0801038663629609, 0.0821679128706957, 0.102016245080356, 
    0.00257927053679852, 0.104804713995872, 0.0647829124718638, 
    0.0933815405966441, 0, 0.0885305184947283, 0.0156636820758888, 
    0.00964308330581547), upper = c(0.0395993826580026, 0, 0, 
    0, 0, 0, 0, 0.0144608848128486, 0, 0, 0, 0, 0.100190439647013, 
    0.0283943307149945, 0, 0, 0.0388989873758117, 0.103806785301648, 
    0.216393593627934, 0.134652492242485, 0.0803909708533288, 
    0.060786906326309, 0, 0.0144608848128486, 0.240155543449604, 
    0.0417905453118005, 0.0209934069818733, 0.0152010393389191, 
    0.0262114066848864, 0.18725297629879, 0.0154171174742423, 
    0.16787027629016, 0.135261417320478, 0.0730453301936332, 
    0.0535952364591485, 0.0152477021748446, 0.130293728302962, 
    0.180409520910527, 0.0325256827586574, 0.0260860226674357, 
    0.239445428925307, 0.0692891343775288, 0.124376503277728, 
    0.178440389529858, 0.197249034916019, 0.0734921511891968, 
    0, 0, 0.384437031638094, 0.162556807583924, 0.134834088604075, 
    0.161335698456987, 0.0380366218751542, 0.213319166758368, 
    0.0939299569941922, 0.149197201634473, 0, 0.12612765068256, 
    0.0371822745939381, 0.014497511518441)), .Names = c("Year", 
"Month", "Mean_cpue", "SD", "CV", "Number", "lower", "upper"), row.names = c(NA, 
-60L), class = "data.frame")

我的图表代码:

xyplot(Mean_cpue~as.factor(Year)|as.factor(Month) ,data=meancpue,xlab="Year", ylab="Index",col="black",
          panel=function(x,y,...){
              panel.xyplot(x, y) 
              panel.lines(meancpue$lower~ as.factor(meancpue$Year), type="l", col = "red", lwd = 2)
          panel.lines(meancpue$upper~ as.factor(meancpue$Year),  type="l",col = "blue", lwd = 2) 
              } 
        ,
        scales=list(y=list(alternating=1,cex=0.8,relation="free"),x=list(alternating=1,cex=.8,rot=90)),
        strip = strip.custom(bg="white" , strip.levels = T))

现在似乎所有小组都在绘制所有不论月份的置信区间。我试过在网上搜索,但仍然有问题。

1 个答案:

答案 0 :(得分:1)

我在xyplot中了解了下标并想出了它。这是我想要的图表的代码:

xyplot(Mean_cpue~as.factor(Year)|as.factor(Month) ,data=meancpue,xlab="Year", ylab="Index",col="black",
          panel=function(x,y,subscripts){
              panel.xyplot(x, y,type="l",col="black",lty=1) 
          panel.xyplot(as.factor(meancpue$Year)[subscripts], meancpue$lower[subscripts], type="l", col = "red", lty = 3,lwd=1)
          panel.xyplot(as.factor(meancpue$Year)[subscripts], meancpue$upper[subscripts], type="l", col = "red", lty=3,lwd = 1)
              } 
        ,
        scales=list(y=list(alternating=1,cex=0.8,relation="free"),x=list(alternating=1,cex=.8,rot=90)),
        strip = strip.custom(bg="white" , strip.levels = T),subscripts=T)