像这样的东西怎么样:
# required packages
library(VGAM)
require(reshape2)
require(ggplot2)
# fitted values from vgam
fit4 <- vgam(BMI ~ s(age, df = c(4, 2)), lms.bcn(zero = 1), data = bmi.nz, trace = TRUE)
fitted.values <- data.frame(qtplot.lmscreg(fit4, percentiles = c(5,50,90,99))$fitted.values)
fitted.values[, 'age'] <- bmi.nz[, 'age']
# melt data.frame
dmelt <- melt(fitted.values, id.vars='age')
# ploting
ggplot(dmelt, aes(age, value, group=variable)) +
geom_line(color='blue') +
annotate(geom='text',
x = max(bmi.nz[, 'age']) + 3,
y = unlist(fitted.values[which.max(fitted.values[, 'age']), -ncol(fitted.values)]),
label=c(' 5%', '50%', '90%', '99%')) +
lapply(2:8*10, function(i) {
annotate(geom='text',
x = i,
y = 1+unlist(fitted.values[which.min(abs(fitted.values[, 'age'] - i)), -ncol(fitted.values)]),
label=paste0(round(unlist(fitted.values[which.min(abs(fitted.values[, 'age'] - i)), -ncol(fitted.values)]),1), '%'))
}) +
scale_y_continuous('BMI') +
theme_bw(base_size=16)