Michelle
Michelle

Reputation: 1363

How do I change line thickness in denscomp plots from the fitdistrplus package in R?

I'm over-plotting three densities onto my data histogram, using denscomp in the fitdistrplus package in R. The code below is working perfectly, but I don't know how to make the lines thicker.

denscomp(list(TryWeibull, TryGamma, TryLognormal), legendtext = plot.legend, 
     fitcol = c("indianred3","gray38", "darkblue"), fitlty = c("dashed", "longdash", "dotdash"),
     xlab = "Age", ylab  = "Proportion", main="")

fitcol is giving me the correct colours, fitly is giving me the correct line types, but I can't work out the command to make the lines thicker. I have two distribution densities that are close together and I have been unsuccessful in clearly identifying them using colour/line type differences. The current graph.

I am trying to de-emphasize the Weibull and emphasise the gamma and lognormal. The proportions are estimates, so I am trying to fit the general shape, not the exact values.

I can't see an option in the denscomp function to specify line widths. I would rather not use the ggplot option, but can shift to that if required. I was hoping there was a function option I'm overlooking.

Edited to add: I raised this as a feature request on GitHub and it has been implemented into the package.

Upvotes: 1

Views: 1303

Answers (2)

Bruce Wang
Bruce Wang

Reputation: 43

I had the same question and followed Edward's solution, which was great and I learnt a lot, but it turned out you can just use ggplot to do that.

denscomp(..., plotstyle = "ggplot") + geom_line(linetype = "dashed",size = 1))

Upvotes: 1

Edward
Edward

Reputation: 18739

Although the author of this package allows you to specify multiple line types (fitlty) and line colours (fitcol), they didn't allow you to specify multiple line widths. But since R is open-source, you are free to modify the function in any way.

Type the following at the R console:

fix(denscomp)

Then add a new argument to the function after fitcol, called fitlwd.

..., fitcol, fitlwd, addlegend = TRUE, ...

Then after line 30 add the following:

if (missing(fitlwd)) 
    fitlwd <- 1

Then after line 34 add the following:

fitlwd <- rep(fitlwd, length.out = nft)

Then modify line 136 as follows:

col = fitcol[i], lwd=fitlwd[i], ...)

Finally, modify line 142:

col = fitcol, lwd=fitlwd,

Save and call the new function as before but now specifying the fitlwd argument:

denscomp(..., fitlwd=c(1,3,3))

enter image description here

Upvotes: 1

Related Questions