It is possible to change line width for smoother l

2019-07-30 17:22发布

问题:

I would like to customise diagnostic plots in ggplot2. I have tried this:

library(ggfortify)
library(ggplot2)
model1 <- lm(len~dose*supp, data = ToothGrowth)
autoplot(model1, which = 1, label.size = 3, data = ToothGrowth, size=3, colour = "dose",
     smooth.colour = 'darkblue', smooth.linetype="dotted", smooth.linesize=3)

I got this picture:

I changed the line colour and line type for smoother line, but I do not know how can I change the line width. I have tried "smooth.linesize", but it is not working. It is possible somehow change the line width?

Can somebody help me? Thank You.

回答1:

One option would be to add another layer to your plot since autoplot.lm does not provide the desired argument smooth.linesize. The first part generates your plot, p.

p <- autoplot(model1, which = 1, label.size = 3, data = ToothGrowth, size=3, colour = "dose",
     smooth.colour = 'darkblue', smooth.linetype="dotted")

The desired data of the smoothed line can now be found somewhere deep inside p here: p@plots[[1]]$layers[[2]]$data

This can be used as the data argument in the call to geom_*. The following code generates the plot below.

p + geom_line(data = p@plots[[1]]$layers[[2]]$data, aes(x = x, y = y), linetype = 3, col = "red", size = 3)

Another option would be to add the not yet existing aesthetics parameter size to the respective layer of the plot object yourself. I guess this option is closer to what you want to achieve.

This can be done via

p@plots[[1]]$layers[[2]]$aes_params$size <- 3
p

I hope this helps.



标签: r ggplot2