3

I hope some more knowledged people in R can help.

Problem: I am trying for some time now to plot a Weibull probability plot with ggplott2. The 2-two factor Weibull should appear as a straight line in a double-logarithmic plot. But it is not straight or the line is gone... An example would be something like this https://www.mathworks.com/help/stats/wblplot.html

Background: I am doing some living calculations with the flexsurv and the survival packages. But I would like to use the ggplot2 environment to display it with more bells and whistles. Finally, I would like to add the failures to the line which was fitted in prior calculations.

All suggestions are welcome. :)

Here my sample code:

library(ggplot2)

ggplot(data.frame(x=c(0,30)), aes(x=x)) +
  stat_function(fun = dweibull, args = list(shape = 2, scale = 15)) 
  #coord_trans(x = "log10", y = "log10") # without the option the function appears, but not as straight line 
Marvin
  • 31
  • 2
  • actually your code should be fine, just produce right sample data: replace `c(0,30)` with something from the `rweibull()`, reactivate `coord_trans` and you should get a line. you just dont have weibull distributed sample data – mischva11 Aug 18 '18 at 18:56

2 Answers2

2

QQ Plot

What you're looking for (from that Matlab example) is a Q-Q plot, which ggplot can do via geom_qq or stat_qq (docs here: https://ggplot2.tidyverse.org/reference/geom_qq.html):

ggplot(data.frame(x = rweibull(n = 100, shape = 1.2, scale = 1.5)), aes(sample = x)) + 
  stat_qq(distribution = stats::qweibull, dparams = list(shape = 1.2, scale = 1.5))

Good luck!

twedl
  • 1,588
  • 1
  • 17
  • 28
0

Thanks for the replies! Frankly speaking it did not quite manage to make it work the way I intended. This should not say something about the suggestions for the solution, but maybe about the approaches I took with ggplot2.

The high-level goal was to generate a Weibull “Time to Failure” plot very similar to the one reference in the initial question. Here the solution which worked very nicely for me: WeibullR, which is recently available on CRAN, http://www.openreliability.org/weibull-r-weibull-analysis-on-r/ It uses the approach of "The New Weibull Handbook, Fifth Edition" by Dr. Robert B. Abernethy.

I am not sure how to handle the answer in the context of sackoverflow, because the answer took a different turn than the question...

Installation for macOS needs some steps, but is in my opinion, wroth while due to the figure quality and how fast the analysis went:

In the terminal:

brew install gcc

For macOS compilations of some module in C++ is necessary: To point R to the right compiler add in ~/.R/Makevars

VER= 
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
# In my case
FLIBS=-L/usr/local/Cellar/gcc/8.2.0/lib/gcc/8

In R console:

install.packages("Rcpp")
install.packages("RcppArmadillo")
install.packages("abrem", repos="http://R-Forge.R-project.org")
Marvin
  • 31
  • 2