0

I am using ggplot2 to make a statistical control chart. I'm unaware if there is a way to do this in r without ggplot (there probably is), but I'm far enough into my code where I think I can finish the job with one fix.

I have the chart created with the data, CL, UCL, and LCL. The only thing I want to add is a label for the CL, UCL, LCL, either right above or to the right of the lines themselves (like you would see in a minitab output).

The code below is how I created my chart. It all looks fine. I've tried label = "UCL = ..." within the geom_hline() parameters, but I'm not too familiar with geom_hline so I don't really know what else to try.

    ggplot(data = wdi, aes(x = wdi$year, y = `wdi$gdp_pc_growth)) + 
     ggtitle("Control Chart: GDP Per Capita Growth (annual %)") + 
     xlab("Year") + ylab("Summary Statistics") +
     scale_x_continuous(breaks = seq(from = 1990, to = 2016, by = 
     1)) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) 
     + geom_line() + 
     geom_hline(yintercept = gdp_growth_mean) + 
     geom_hline(yintercept = ucl, linetype = "twodash") + 
     geom_hline(yintercept = lcl, linetype = "twodash")

I'd like it to look similar to a minitab control chart, with the CL, UCL, and LCL displayed either at the top right of, or directly to the right of each control line. Thanks.

rm1512
  • 11
  • 6
  • 2
    Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including the data in `wdi` as plain text, using _e.g._ `dput()`. – neilfws Jul 26 '19 at 01:48
  • Try adding `annotate(geom = 'text', x = , y = , label = )` where x and y specify where you'd like your label. Check `?ggplot2::annotate` – Tony Ladson Jul 26 '19 at 01:49
  • Also FYI yes, there are packages for control charts in R _e.g._ [qicharts](https://cran.r-project.org/web/packages/qicharts/vignettes/controlcharts.html), [qcc](https://cran.r-project.org/web/packages/qcc/vignettes/qcc_a_quick_tour.html). – neilfws Jul 26 '19 at 01:56
  • Looking up minitab, it seems you want the UCL labels outside the plot. I would suggest creating a secondary y-axis on the right side, with breaks and labels at your various y-intercepts. E.g. something like: `scale_y_continuous(sec.axis = sec_axis(~., breaks = c(lcl, gdp_growth_mean,ucl), labels = c("LCL", "CL", "UCL")))` (not tested) – David Klotz Jul 26 '19 at 01:56

0 Answers0