r - 如何在 R 中的中值控制图的垂直线上绘制点

标签 r plot charts median qcc

需要创建一个如下所示的图表: enter image description here

我快到了,只是缺少垂直绘制的各个点。

这里是数据:

q6 <- structure(list(x1 = c(0.0629, 0.063, 0.0628, 0.0634, 0.0619, 
0.0613, 0.063, 0.0628, 0.0623, 0.0631, 0.0635, 0.0623, 0.0635, 
0.0645, 0.0619, 0.0631, 0.0616, 0.063, 0.0636, 0.064, 0.0628, 
0.0615, 0.063, 0.0635, 0.0623), x2 = c(0.0636, 0.0631, 0.0631, 
0.063, 0.0628, 0.0629, 0.0639, 0.0627, 0.0626, 0.0631, 0.063, 
0.063, 0.0631, 0.064, 0.0644, 0.0627, 0.0623, 0.063, 0.0631, 
0.0635, 0.0625, 0.0625, 0.0632, 0.0629, 0.0629), x3 = c(0.064, 
0.0622, 0.0633, 0.0631, 0.063, 0.0634, 0.0625, 0.0622, 0.0633, 
0.0633, 0.0638, 0.063, 0.063, 0.0631, 0.0632, 0.063, 0.0631, 
0.0626, 0.0629, 0.0629, 0.0616, 0.0619, 0.063, 0.0635, 0.063), 
    x4 = c(0.0635, 0.0625, 0.0633, 0.0632, 0.0619, 0.0625, 0.0629, 
    0.0625, 0.063, 0.0631, 0.0635, 0.0627, 0.063, 0.064, 0.0622, 
    0.0628, 0.062, 0.0629, 0.0635, 0.0635, 0.062, 0.0619, 0.0631, 
    0.0631, 0.0626), x5 = c(0.064, 0.0627, 0.063, 0.0633, 0.0625, 
    0.0628, 0.0627, 0.0627, 0.0624, 0.063, 0.0633, 0.0629, 0.063, 
    0.0642, 0.0635, 0.0629, 0.0625, 0.0628, 0.0634, 0.0634, 0.0623, 
    0.0622, 0.063, 0.0633, 0.0628)), .Names = c("x1", "x2", "x3", 
"x4", "x5"), class = "data.frame", row.names = c(NA, -25L))

代码如下:

range_span <- function(x) return(diff(range(x))) # function to calculate range
# q6 <- read.table(file="/Users/.../blah.csv",header=T,sep=",") #data
medians <- apply(q6,1,median) 
ranges <- apply(q6,1,range_span)
centre <- mean(medians) #grand median
Rtilde <- median(ranges) #median of ranges

plot(medians, type="b",xaxp=c(1, 25, 24),pch=19,xlab="Sample No.",ylab="Medians",main="Median Chart for Thickness of Metal Parts")

# code below draws the control limits 

action.limits<-c(centre+0.681*Rtilde,centre-0.681*Rtilde)
warn.limits<-c(centre+(2/3)*0.681*Rtilde,centre-(2/3)*0.681*Rtilde)

abline(h = centre, lty = 3, col = "black")  
v0 <-c("CL") 
mtext(side = 4, text = v0, at = centre, col = "black", las=2)

abline(h = warn.limits, lty = 3, col = "blue") 
v1 <-c("UWL","LWL") 
mtext(side = 4, text = v1, at = warn.limits, col = "blue", las=2) 

abline(h = action.limits, lty = 3, col = "black") 
v2 <-c("UCL","LCL") # the labels for action.limits
mtext(side = 4, text = v2, at = action.limits, col = "black", las=2)

我确信有一个简单的解决方案,我对 R 一点经验都没有。我只是想通过在 R 中为类(class)作业制作图表来给自己一个挑战,但现在我开始没时间了。

  • points() 有帮助吗?我需要 R 来识别 q6 中的每一行都是一个样本,这样我就可以做 points(q6,c(1:25)) 或类似的事情?

最佳答案

因此,您的数据中的任何位置都没有 x 坐标(1 到 25),这会使绘图变得困难。

q6$x = 1:25

下一步并不是绝对必要的,但它使事情变得更方便。我要把你的数据“融合”成长格式:

q6_long = reshape2::melt(q6, id.vars = "x")

## just showing what it looks like now
head(q6_long)
#   x variable  value
# 1 1       x1 0.0629
# 2 2       x1 0.0630
# 3 3       x1 0.0628
# 4 4       x1 0.0634
# 5 5       x1 0.0619
# 6 6       x1 0.0613

现在使用 points 函数可以轻松添加点:

points(q6_long$x, q6_long$value, pch = 5)

enter image description here

关于r - 如何在 R 中的中值控制图的垂直线上绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34949996/

相关文章:

r - 循环基础 R 以获得每个区域的 pdf 图或该图的 ggplot 替代方案

ios - 如何将图例添加到 SwiftChart 库

javascript - 谷歌折线图 : Change color when line down

c - 使用 C 和并行化在 R 中快速关联

python - 高效的 Matplotlib 重绘

r - 如何将这个宽数据帧转换为这个长数据帧?

r - 在图的顶部添加图例

c# - 在 winform 项目中向图表添加十字准线

r - 将 'model' 带出地球

javascript - R Shiny 的selectizeInput : set minimum for amount of inputs