ggplot2 ggplot() 分组回归,分组填充柱形图,设置stat_xxx,增添残差回归图层
dplot <- ggplot(diamonds, aes(clarity, fill = cut)) ##按cut类 计数填充 dplot + geom_bar(position = "stack")##条形图 分类 dplot + geom_bar(position = "fill")##等高条形图 dplot + geom_bar(position = "dodge")##并排分类条形图 p <- ggplot(Oxboys, aes(age, height, group = Subject)) + geom_line() p + geom_smooth(aes(group = Subject), method="lm", se = F) p + geom_smooth(aes(group = 1), method="lm", size = 2, se = F) #########################上下相同###################### qplot(age, height, data=Oxboys, group = Subject, geom="line") + geom_smooth(method="lm", se = F) qplot(age, height, data=Oxboys, group = Subject, geom="line") + geom_smooth(aes(group = 1), method="lm", size = 2, se = F) #################################################### qplot(Occasion, height, data=Oxboys, geom="boxplot") qplot(Occasion, height, data=Oxboys, geom="boxplot") + geom_line(aes(group = Subject), colour="#3366FF") #######叠加图层 d <- ggplot(diamonds, aes(carat)) + xlim(0, 3) d + stat_bin(aes(ymax = ..count..), binwidth = 0.1, geom = "area") d + stat_bin( aes(size = ..density..), binwidth = 0.1, geom = "point", position="identity" ) stat_bin is suitable only for continuous x data. If your x data is discrete, you probably want to use stat_count. p <- ggplot(diamonds,aes(carat)) + xlim(0, 3) p + geom_histogram(position = 'identity', alpha=0.5, aes(y = ..density.., fill = factor(color))) + stat_density(geom = 'line', position = 'identity', aes(colour = factor(color)))
model <- lme(height ~ age, data = Oxboys, random = ~ 1 + age | Subject) oplot <- ggplot(Oxboys, aes(age, height, group = Subject)) + geom_line() age_grid <- seq(-1, 1, length = 10) subjects <- unique(Oxboys$Subject) preds <- expand.grid(age = age_grid, Subject = subjects) ##扩充data frame 形成完整的 preds$height <- predict(model, preds) oplot + geom_line(data = preds, colour = "#3366FF", size= 0.4) Oxboys$fitted <- predict(model) ##向Oxboys里添加fitted列 Oxboys$resid <- with(Oxboys, fitted - height) ##with 内调data,后边是进行相应的变量操作 oplot %+% Oxboys + aes(y = resid) + geom_smooth(aes(group=1)) ##同样的oplot采用不同的数据绘图,并更改图层设置,height变为resid 残差,并增添总体回归线