在R中的箱形图中添加不同的百分位数 [英] Adding Different Percentiles in boxplots in R

查看:232
本文介绍了在R中的箱形图中添加不同的百分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,最近用它来制作一些BoxLots。我还在我的boxplot中添加了平均值和标准偏差。我想知道是否可以在不同的百分位数上添加某种刻度标记或圆圈。比方说,如果我想在每个HOUR boxplot中标记第85个,第90个百分点,有没有办法做到这一点?我的数据包括在每小时和一小时内以兆瓦为单位的一年的装载量。我的输出包括每个月每个小时24个箱形图。我每个月都在做一次,因为我不确定是否有办法一次运行所有96个(每个月,平日/周末,4个不同的区域)箱形图。

  JANWD< -read.csv(C:\\我的电脑 \\ MWBox2.csv)
JANWD.df< -data.frame(JANWD)
JANWD.sub< -subset(JANWD.df,MONTH< 2& weekend ==NO)

KeepCols< -c(Hour,Houston_Load)
HWD< - JANWD.sub [,KeepCols]

sd< -tapply (HWD $ Houston_Load,HWD $ Hour,sd)
表示< -tapply(HWD $ Houston_Load,HWD $ Hour,mean)

boxplot(Houston_Load〜Hour,data = HWD,xlab =WEEKDAY HOURS,ylab =MW Differnce,ylim = c(-10,20),smooth = TRUE,col =bisque,range = 0)

points(sd,pch = 22,col =blue)
points(means,pch = 23,col =red)

#输出用于运行1月份boxplot的数据的子集休斯顿
str(HWD)
'data.frame':504 obs。 2个变量:
`$小时:int 1 2 3 4 5 6 7 8 9 10 ...'
`$ Houston_Load:num 1.922 2.747 -2.389 0.515 1.922 ...'

#原始数据的$ OUTPUT
str(JANWD)
'data.frame':8783 obs。 9个变量:
$日期:因子w / 366个等级1/1/2012,1/10/2012,..:306 306 306 306 306 306 306 306 306 306 ...
`$ Hour:int 1 2 3 4 5 6 7 8 9 10 ...'
`$ MONTH:int 8 8 8 8 8 8 8 8 8 ...'
`$周末:因子W / 2级别NO,YES:1 1 1 1 1 1 1 1 1 1''
`$ TOTAL_LOAD:num 0.607 5.111 6.252 7.607 0.607 ...'
$ $ Houston_Load:num -2.389 0.515 1.922 2.747 -2.389 ...'
`$ North_Load:num 2.95 4.14 3.55 3.91 2.95 ...'
`$ South_Load:num -0.108 0.267 0.54 0.638 - 0.108 ...'
`$ West_Load:num 0.154 0.193 0.236 0.311 0.154 ...'


解决方案

以下是一种使用 quantile()来计算相关百分位数的方法。我使用 rug()添加标记。

  set.seed 1)
X < - rnorm(200)
boxplot(X,yaxt =n)

##计算所需分位数
qntl < - quantile(X,probs = c(0.85,0.90))

##将它们作为左边的
rug(qntl,side = 2,col =blue ,lwd = 2)

##添加方框和轴
轴(2)
box()
$ b

更新:为了响应OP提供 str()输出,这里是一个类似于OP必须处理的数据的例子:

  set.seed(1)## make reproducible 
HWD< - data.frame(Hour = rep(0:23,10),
Houston_Load = rnorm(24 * 10))

现在假设您希望每个小时的第85百分位和第90百分位数的分位数?如果是这样,我们需要按照前面显示的 Hour 并通过 quantile()进行计算。

  quants < -  sapply(split(HWD $ Houston_Load,list(HWD $ Hour)),
quantile,probs = c (0.85,0.9))

给出:

  R> (HWD $ Houston_Load,列表(HWD $小时)),
+分位数,probs = c(0.85,0.9))
R>数量
0 1 2 3 4 5 6
85%0.3576510 0.8633506 1.581443 0.2264709 0.4164411 0.2864026 1.053742
90%0.6116363 0.9273008 2.109248 0.4218297 0.5554147 0.4474140 1.366114
7 8 9 10 11 12 13 14
85%0.5352211 0.5175485 1.790593 1.394988 0.7280584 0.8578999 1.437778 1.087101
90%0.8625322 0.5969672 1.830352 1.519262 0.9399476 1.1401877 1.763725 1.102516
15 16 17 18 19 20 21
85%0.6855288 0.4874499 0.5493679 0.9754414 1.095362 0.7936225 1.824002
90%0.8737872 0.6121487 0.6078405 1.0990935 1.233637 0.9431199 2.175961
22 23
85%1.058648 0.6950166
90%1.145783 0.8436541

现在我们可以在箱形图的x个位置绘制标记

  boxplot(Houston_Load〜Hour,data = HWD,axes = FALSE)
xlocs< - 1: 24 ##在哪里绘制标记
tickl < - 0.15 ##所用标记的长度
y1 = quants [,i],
x1 = rep(xlocs [i] + 0.15,2),y1 = quants [,i],
col = c (xlab =Hour,ylab =Houston Load)
轴(1,at = xlocs, labels = xlocs - 1)
axis(2)
box()
legend(bottomleft,legend = paste(c(0.85,0.90),quantile) ,
bty =n,lty =solid,lwd = 2,col = c(red,blue))

得到的数字应该如下所示:


I am failry new to R and recently used it to make some Boxplots. I also added the mean and standard deviation in my boxplot. I was wondering if i could add some kind of tick mark or circle in different percentile as well. Let's say if i want to mark the 85th, $ 90th percentile in each HOUR boxplot, is there a way to do this? My data consist of a year worth of loads in MW in each hour & My output consist of 24 boxplots for each hour for each month. I am doing each month at a time because i am not sure if there is a way to run all 96(Each month, weekday/weekend , for 4 different zones) boxplots at once. Thanks in advance for help.

JANWD <-read.csv("C:\\My Directory\\MWBox2.csv")
JANWD.df<-data.frame(JANWD)
JANWD.sub <-subset(JANWD.df, MONTH < 2 & weekend == "NO")

KeepCols <-c("Hour" , "Houston_Load")
HWD <- JANWD.sub[ ,KeepCols]

sd <-tapply(HWD$Houston_Load, HWD$Hour, sd)
means <-tapply(HWD$Houston_Load, HWD$Hour, mean)

boxplot(Houston_Load ~ Hour, data=HWD, xlab="WEEKDAY HOURS", ylab="MW Differnce", ylim= c(-10, 20), smooth=TRUE ,col ="bisque", range=0)

points(sd, pch = 22, col= "blue")
points(means, pch=23, col ="red")

#Output of the subset of data used to run boxplot for month january in Houston 
str(HWD)
'data.frame':   504 obs. of  2 variables:
 `$ Hour        : int  1 2 3 4 5 6 7 8 9 10 ...'
 `$ Houston_Load: num  1.922 2.747 -2.389 0.515 1.922 ...'

#OUTPUT of the original data
str(JANWD)
'data.frame':   8783 obs. of  9 variables:
 $ Date        : Factor w/ 366 levels "1/1/2012","1/10/2012",..: 306 306 306 306 306 306 306 306 306 306 ...
 `$ Hour        : int  1 2 3 4 5 6 7 8 9 10 ...'
` $ MONTH       : int  8 8 8 8 8 8 8 8 8 8 ...'
 `$ weekend     : Factor w/ 2 levels "NO","YES": 1 1 1 1 1 1 1 1 1 1 ...'
 `$ TOTAL_LOAD  : num  0.607 5.111 6.252 7.607 0.607 ...'
 `$ Houston_Load: num  -2.389 0.515 1.922 2.747 -2.389 ...'
 `$ North_Load  : num  2.95 4.14 3.55 3.91 2.95 ...'
 `$ South_Load  : num  -0.108 0.267 0.54 0.638 -0.108 ...'
 `$ West_Load   : num  0.154 0.193 0.236 0.311 0.154 ...'

解决方案

Here is one way, using quantile() to compute the relevant percentiles for you. I add the marks using rug().

set.seed(1)
X <- rnorm(200)
boxplot(X, yaxt = "n")

## compute the required quantiles
qntl <- quantile(X, probs = c(0.85, 0.90))

## add them as a rgu plot to the left hand side
rug(qntl, side = 2, col = "blue", lwd = 2)

## add the box and axes
axis(2)
box()

Update: In response to the OP providing str() output, here is an example similar to the data that the OP has to hand:

set.seed(1) ## make reproducible
HWD <- data.frame(Hour = rep(0:23, 10),
                  Houston_Load = rnorm(24*10))

Now get I presume you want ticks at 85th and 90th percentiles for each Hour? If so we need to split the data by Hour and compute via quantile() as I showed earlier:

quants <- sapply(split(HWD$Houston_Load, list(HWD$Hour)),
                 quantile, probs = c(0.85, 0.9))

which gives:

R> quants <- sapply(split(HWD$Houston_Load, list(HWD$Hour)),
+                  quantile, probs = c(0.85, 0.9))
R> quants
            0         1        2         3         4         5        6
85% 0.3576510 0.8633506 1.581443 0.2264709 0.4164411 0.2864026 1.053742
90% 0.6116363 0.9273008 2.109248 0.4218297 0.5554147 0.4474140 1.366114
            7         8        9       10        11        12       13       14
85% 0.5352211 0.5175485 1.790593 1.394988 0.7280584 0.8578999 1.437778 1.087101
90% 0.8625322 0.5969672 1.830352 1.519262 0.9399476 1.1401877 1.763725 1.102516
           15        16        17        18       19        20       21
85% 0.6855288 0.4874499 0.5493679 0.9754414 1.095362 0.7936225 1.824002
90% 0.8737872 0.6121487 0.6078405 1.0990935 1.233637 0.9431199 2.175961
          22        23
85% 1.058648 0.6950166
90% 1.145783 0.8436541

Now we can draw marks at the x locations of the boxplots

boxplot(Houston_Load ~ Hour, data = HWD, axes = FALSE)
xlocs <- 1:24 ## where to draw marks
tickl <- 0.15 ## length of marks used
for(i in seq_len(ncol(quants))) {
    segments(x0 = rep(xlocs[i] - 0.15, 2), y0 = quants[, i],
             x1 = rep(xlocs[i] + 0.15, 2), y1 = quants[, i],
             col = c("red", "blue"), lwd = 2)
}
title(xlab = "Hour", ylab = "Houston Load")
axis(1, at = xlocs, labels = xlocs - 1)
axis(2)
box()
legend("bottomleft", legend = paste(c("0.85", "0.90"), "quantile"),
       bty = "n", lty = "solid", lwd = 2, col = c("red", "blue"))

The resulting figure should look like this:

这篇关于在R中的箱形图中添加不同的百分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆