【问题标题】:Add standard deviation as grey smoothed bands to lattice xyplot将标准偏差作为灰色平滑带添加到晶格 xyplot
【发布时间】:2015-04-21 09:41:26
【问题描述】:

我试图解决这个问题已经有一段时间了,但我无法弄清楚......我相信这很简单。 我想将 +/- 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)

我想要像 xyplot 上的灰色带这样的结果R - Lattice xyplot - How do you add error bars to groups and summary lines?。

有什么帮助吗? 提前致谢!

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    创建灰色平滑线是一个棘手的问题 - 你最好的出价是使用来自latticeExtra package 的panel.smoother,但它有很多奇怪的参数,所以它不容易开箱即用。

    但是,使用来自Hmisc package 的xYplot 可以轻松地从您的数据中得出典型的标准差条形图。

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

    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  
    

    比你在xYplot 中的Cbind 函数中添加这些限制。你需要增加你的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)
    

    【讨论】:

    • 非常感谢!这不是我一开始想要的,但它解决了我显示 sd 的问题 :)
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多