Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show 2 standard deviations on a ggplot2 control chart (in addition to the normal 3)

Tags:

r

ggplot2

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.

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.

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).

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. 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? It'd look something like this:

[image

like image 279
stackinator Avatar asked Jan 24 '26 12:01

stackinator


1 Answers

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()

enter image description here

like image 59
zlipp Avatar answered Jan 27 '26 00:01

zlipp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!