将标准偏差作为灰度平滑条带添加到格子xyplot

时间:2015-04-21 09:41:26

标签: r lattice

这是我试图解决这个问题的时间,我无法弄明白......我相信这很简单。 我想将+/- MM标准差(列SD.MM)添加到我的MM行作为它上面/下面的一行,并且对于KK一行添加相同。 这是我的数据

data <- read.table(textConnection(
    "Day         Period  MM      KK     SD.MM   SD.KK
1   Tuesday     1   20.0    131.0   1.74    11.40
2   Tuesday     2   76.8    203.0   10.60   28.10
3   Tuesday     3   26.7    118.0   13.00   57.50
4   Wednesday   1   33.8    143.0   2.64    11.20
5   Wednesday   2   74.1    232.0   10.30   32.60
6   Wednesday   3   34.0    130.0   17.60   67.00
7   Thursday    1   46.4    203.0   3.95    16.80
8   Thursday    2   59.5    165.0   8.26    23.10
9   Thursday    3   32.1    120.0   16.50   61.90
10  Friday      1   24.6    98.8    1.96    7.95
11  Friday      2   139.0   367.0   18.20   47.90
12  Friday      3   91.4    216.0   20.90   49.40
13  Saturday    1   30.6    158.0   2.42    11.60
14  Saturday    2   78.2    295.0   8.02    33.40
15  Saturday    3   90.7    310.0   51.60   176.00
16  Sunday      1   28.0    115.0   2.47    9.91
17  Sunday      2   80.4    232.0   10.90   30.40
18  Sunday      3   42.5    211.0   19.70   97.80"),
header=TRUE,as.is=TRUE)

这里是生成下面图像的代码。

library(lattice)
library(gridExtra)
a <- xyplot(MM~Period|Day,data=data,layout=c(6,1), type="o", 
            xlab="Period", 
            ylab="Loads",
            ylab.right = "Loads",
            main ="Daily trend",
            index.cond=list(c(5,6,4, 1:3)),
            ylim = c(0,150),
            scales=list(x=list(at=seq(1,3,1),labels=c("B","F","A")),
                        y=(list(alternating=3))),
            key = list(text = list("MM"), points = list(pch=1, col="steelblue2"),
                       text = list("KK"), points = list(pch=1, col="violet"),
                       text = list("d-KK = 306"), lines = list(lty=4, lwd=2, col="green4"),
                       text = list("u-KK = 312"), lines = list(lty=4, lwd=2, col="red"),
                       text = list("s-KK = 41"), lines = list(lty=4, lwd=2, col="gold"),
                       border = F
            )
)

b <- xyplot(KK~Period|Day,data=data,layout=c(6,1), type="o", col="violet",
            xlab="Period", 
            ylab="Loads",
            ylab.right = "Loads",
            index.cond=list(c(5,6,4, 1:3)), 
            ylim = c(0,400),
            scales=list(x=list(at=seq(1,3,1),labels=c("B","F","A")),
                        y=(list(alternating=3))),
            panel = function(x,y,...) {
              panel.abline(h=306,lty = 4, lwd=2, col="green4")
              panel.abline(h = 312, lty = 4, lwd=2, col="red")
              panel.abline(h = 41, lty = 4, lwd=2, col="gold")
              panel.xyplot(x,y,...)
            }
)
grid.arrange(a,b, nrow=2)

MM and KK xyplot

我希望像xyplot上的灰色条带一样R - Lattice xyplot - How do you add error bars to groups and summary lines?

有任何帮助吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

创建一条灰色流畅的线条是一个棘手的问题 - 你的最佳出价是使用来自latticeExtra packagepanel.smoother,但它有很多奇怪的参数,所以它不容易被破解出来框。

但是,使用Hmisc package中的xYplot可以轻松地从您的数据中执行标准偏差条。

首先,您必须根据相应的数据定义标准偏差条的下限和上限。我只是在值的两边对称地使用标准偏差,但是根据计算方式可以将它除以2:

data$MM_sd_hi <- data$MM+data$SD.MM  
data$MM_sd_low <- data$MM-data$SD.MM  
data$KK_sd_hi <- data$KK+data$SD.KK  
data$KK_sd_low <- data$KK-data$SD.KK  

Cbind内的xYplot函数中添加这些限制。您需要增加ylim以适应这一点,因为xYplot做了一些奇怪的尺度。这是代码:

library(Hmisc)
library(gridExtra)
a <- xYplot(Cbind(MM, MM_sd_low, MM_sd_hi)~Period | Day, data=data, layout=c(6,1), 
            type="o", xlab="Period",ylab="Loads", ylab.right = "Loads", 
            main ="Daily trend", index.cond=list(c(5,6,4, 1:3)), ylim = c(0,170),
            scales=list(x=list(at=seq(1,3,1),labels=c("B","F","A")),
                        y=(list(alternating=3))),
            key = list(text = list("MM"), points = list(pch=1, col="steelblue2"),
                       text = list("KK"), points = list(pch=1, col="violet"),
                       text = list("d-KK = 306"), lines = list(lty=4, lwd=2, col="green4"),
                       text = list("u-KK = 312"), lines = list(lty=4, lwd=2, col="red"),
                       text = list("s-KK = 41"), lines = list(lty=4, lwd=2, col="gold"),
                       border = F)
           )   

b <- xYplot(Cbind(KK, KK_sd_low, KK_sd_hi)~Period|Day,data=data,layout=c(6,1), 
            type="o", col="violet", xlab="Period", ylab="Loads", ylab.right = "Loads",
            index.cond=list(c(5,6,4, 1:3)), ylim = c(0,500),
            scales=list(x=list(at=seq(1,3,1),labels=c("B","F","A")),
                        y=(list(alternating=3))),
            panel = function(x,y,...) {
              panel.abline(h=306,lty = 4, lwd=2, col="green4")
              panel.abline(h = 312, lty = 4, lwd=2, col="red")
              panel.abline(h = 41, lty = 4, lwd=2, col="gold")
              panel.xYplot(x,y,...)
            }
)
grid.arrange(a,b, nrow=2)

enter image description here