2

I want to add a vertical abline to the Pareto chart at the points where y is a quantile (see image below). The function I am using for Pareto chart is pareto.chart() in the "qcc" package and ggplot2 package. I am only using pareto.chart to get the cumulative y. I also previously, made the Pareto chart using pareto.chart(). It would be okay if you can create the Pareto with either of those.

My major concern is how to draw vertical lines at quartile. I was able to draw vertical lines at the deciles of the x-axis but not at the quartiles of the y-axis.

Here is the function:

xyz<-pareto.chart(Product, ylab = "Number of Products", xlab="Customer", xaxt="n")
xyz<-data.frame(xyz)

i2<-ggplot(data=xyz, 
           aes(x=seq(1,length(xyz$Frequency)),y=Cum.Percent.,group=1)) + 
    geom_line(colour="red", size=1) + 
    theme_classic() + 
    theme(axis.text.x = element_text(angle = 75, hjust = 1, size=8)) +
    labs(x="Customer",y="Product Share (%)") +
    ggtitle("Pareto Chart") +
    ylim(0,100.1)

print(i2)

Image shows how it is and how I want. Basically, I want red lines at say 50% and 90% or at some decile or quartile of Y enter image description here

Craig
  • 4,492
  • 2
  • 19
  • 22
Abhishek Singh
  • 187
  • 2
  • 13

2 Answers2

0
+ geom_vline(xintercept = rev(stats[trunc(cumsum(xyz$Frequency)*.5) == 50, "x"])[1]
Eduardo Barbaro
  • 411
  • 7
  • 17
  • its creating a vertical line where x=0.5 however i want a line where y=50% – Abhishek Singh Jun 23 '16 at 09:16
  • Thanks mate.wBut its still drawing at the same place. I don't know how – Abhishek Singh Jun 23 '16 at 09:31
  • Try the geom_hline(). – Eduardo Barbaro Jun 23 '16 at 09:35
  • I tried hline y intercept it drew below x axis and also tried hline x intercept it drew the same . What i feel is since i have to draw at 50% wherever that point is in the graph the x-intercept is going to be constant. But for that how to find what should be x intercept at that point where y gets the value 50%. Inverse function would be very tedious. – Abhishek Singh Jun 23 '16 at 09:47
  • I am not able to run it but i think we are getting the y value here which i already know. I want to get the x value for y=50% – Abhishek Singh Jun 23 '16 at 10:15
0

I would use the segments command. It is independent of the other two packages, so the method would work regardless.

The basic premise is to determine the x,y point (or array) to draw from and the x,y point (or array) to draw to.

For example:

segments(
         c(cat1,cat2),0,
         c(cat1,cat2),quantile(data, probs = seq(.5, .9)), 
         col = "red", 
         lty = "solid"
         )

Where cat1 and cat2 are your two categories in your Pareto chart which correspond to the y-axis probabilities. I would need to play with some data to see if I could find an easy way to correlate the categories to the y-axis probabilities, but this method will draw the lines you want. (This method would also work if you wanted to draw segments which were horizontal or two sets of segments for horizontal and vertical lines.)

Tavrock
  • 558
  • 5
  • 15