Aaron C
Aaron C

Reputation: 365

How to use qicharts2 and ggplots2 together?

according to the package page, A qic object Inherits from 'ggplot'. It is also mentioned that the package imports: ggplot2 (≥ 2.2.0).

This suggests there must be a way to combine ggplot2 and qichart2. I have already tried ggplot(data) + qic(details) with no success.

The reason I want to be able to do this is to improve the formatting of the qicharts2 plots. Googling did not result in anything useful for this (i suspect this is the case due qicharts2 only being released this month (February 2018).

Cheers,

Aaron

Edit: this has been solved. The solution is qic returns a ggplot object. Meaning you can do qic() + theme() + etc. As mentioned below too.

Follow up, any way to edit/change the gray square?

Edit2: the gray square is hard coded and so are the limits. However, it is possible to retrieve the results from qic()$ and build your own graph.

Edit3: I ended up using qcc package instead. It gave the same results as minitab, qicharts2 gave different ones. See here for more info on this. While I find the design of qcc unpleasant it is readable.

Upvotes: 2

Views: 1283

Answers (1)

johnm
johnm

Reputation: 320

using chart example from the qicharts2 package help

library(qicharts2)
library(ggplot2)


p <- qic(age,
    data    = tail(cabg, 100), 
    chart   = 'i',
    exclude = c(45, 70),
    title   = 'Age of the last 100 patients (I chart)',
    ylab    = 'Years',
    xlab    = 'Patient #')

p2 <- p$data


ichart <- ggplot(p2,aes(x,y)) +
  geom_ribbon(ymin = p2$lcl,ymax = p2$ucl, fill = "green",alpha = 0.4) +
  geom_line(colour = "blue", size = .75) + 
  geom_line(aes(x,cl)) +
  geom_point(colour = "black" , fill = "black", size = 1.5) +
  ggtitle(label = "example i chart") +
  labs(x = NULL,
       y = NULL)+
  theme_minimal()

ichart

enter image description here

Now with stepped limits, again with data from the package

 step <- qic(month, n,
               data  = cdi,
               chart = 'c',
               part  = 24,
               title = '',
               ylab  = 'Count',
               xlab  = 'Month')

stepped <- step$data

stepped %>% 
  ggplot(aes(x,y)) +
    geom_ribbon(ymin = stepped$lcl,ymax = stepped$ucl, 
    fill = "steel  blue",alpha = 0.4) +
  geom_line(size = .75) + 
  geom_line(aes(x,cl)) +
  geom_point(size = 1.5) +
  ggtitle(label = "example c chart with stepped limits") +
      labs(x = NULL,
       y = NULL) +
  geom_text(aes(label = round(cl.lab,2), y = cl + 1)) +
  theme_minimal() +
  theme(panel.grid.minor = element_blank(),
    panel.grid.major = element_blank()) 

# last line optional

enter image description here

Upvotes: 5

Related Questions