[英]show 2 standard deviations on a ggplot2 control chart (in addition to the normal 3)
First I create the data: 首先我创建数据:
library(ggplot2)
library(ggQC)
set.seed(5555)
Golden_Egg_df <- data.frame(month=1:12, egg_diameter = rnorm(n = 12, mean = 1.5, sd = 0.2))
Then I setup the base ggplot. 然后我设置了基础ggplot。
XmR_Plot <- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
geom_point() + geom_line()
I can create a simple control chart with the ggQC package, in the following manner. 我可以通过以下方式使用ggQC包创建一个简单的控制图。
XmR_Plot + stat_QC(method = "XmR")
I can facet the control chart to show different levels of standard deviation (in this example, between 1-3). 我可以通过控制图来显示不同的标准偏差水平(在本例中,在1-3之间)。
XmR_Plot + stat_qc_violations(method = "XmR")
What I want is to be able to see both 2 and 3 standard deviations on the same chart, not faceted. 我想要的是能够在同一图表上看到2 和 3标准偏差,而不是刻面。 My imagined syntax would be 我想象的语法就是
XmR_Plot + stat_QC(method = "XmR", stand.dev = c(2, 3))
or something like that. 或类似的东西。 But it obviously does not work, how do I get multiple standard deviations to show on 1 chart? 但它显然不起作用,如何在1个图表上显示多个标准偏差? It'd look something like this: 它看起来像这样:
[ [
I highly recommend calculating your summary statistics yourself. 我强烈建议您自己计算摘要统计信息。 You'll get a lot more control over the plot! 你会得到更多的控制情节!
library(ggplot2)
library(dplyr)
library(tidyr)
set.seed(5555)
golden.egg.df = data.frame(month=1:12,
egg_diameter = rnorm(n = 12,
mean = 1.5,
sd = 0.2)
)
lines.df = golden.egg.df %>%
# Calculate all the summary stats
mutate(mean = mean(egg_diameter),
sd = sd(egg_diameter),
plus_one = mean + sd,
plus_two = mean + 2 * sd,
plus_three = mean + 3 * sd,
minus_one = mean - sd,
minus_two = mean - 2 * sd,
minus_three = mean - 3 * sd
) %>%
# Remove what we don't want to plot
select(-month, -egg_diameter, -sd) %>%
# Filter so the dataframe is now one unique row
unique() %>%
# Make the table tall for plotting
gather(key = stat,
value = value) %>%
# Add a new column which indicates how many SDs a line is from
# the mean
mutate(linetype = gsub("[\\s\\S]+?_", "", stat, perl = TRUE))
ggplot(golden.egg.df,
aes(x = month, y = egg_diameter)) +
geom_hline(data = lines.df,
aes(yintercept = value, linetype = linetype)) +
geom_point() +
geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.