如何在ggplot2的同一图中为不同的分位数添加不同的图例? [英] How can I add a different legend for different quantiles in the same graph in ggplot2?

查看:83
本文介绍了如何在ggplot2的同一图中为不同的分位数添加不同的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

           ggplotRegression <- function (fit) {

            require(ggplot2)

           ggplot(fit$model, aes_string(x = names(fit$model)[2], y = 
          names(fit$model)[1])) + 
            geom_point() +
            stat_smooth(method = "lm", col = "red") +
            labs(title = paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 
              5),
                   "Intercept =",signif(fit$coef[[1]],5 ),
                   " Slope =",signif(fit$coef[[2]], 5),
                   " P =",signif(summary(fit)$coef[2,4], 5)))

}

               taus <-c(0.05  , 0.25, 0.50 , 0.75, 0.90 , 0.95)

                m <- ggplotRegression( lm(formula = BMI ~   height_in_m 
                  +weight_in_kg+ Highest_Education_level + 
                   wealth_index + age_in_year_groups, data = dat_new))

             m+geom_quantile(quantiles=taus, lwd=1.5 , col="green4", 
             fill=taus)

现在,我想为每个分位数添加特定的颜色,并为每个分位数添加特定的图例.

解决方案

许多ggplot统计信息使您可以使用..中包含的计算结果,例如,对于geom_density,您可以在..count.. >.

使用geom_quantile,您可以使用..quantile..

df <- data_frame(x = rnorm(100), y = rnorm(100))
ggplot(df, aes(x, y)) + 
  geom_point() + 
  geom_quantile(aes(colour = as.factor(..quantile..))) 

诀窍是找出这些变量的名称.需要计算统计信息的几何体(例如geom_quantilegeom_density)具有关联的ggproto对象,例如StatQuantileStatDensity,该对象在名为compute_group的元素中具有用于计算的代码.

StatQuantile$compute_group的最后一个命令是

    plyr::ldply(quantiles, quant_pred, data = data, method = method, 
    formula = formula, weight = weight, grid = grid, method.args = method.args)

此处的函数quant_pred-您可以通过ggplot2:::quant_pred看到该函数,返回一个列表.此列表的组件,包括quantile,都可以在aes中使用.

           ggplotRegression <- function (fit) {

            require(ggplot2)

           ggplot(fit$model, aes_string(x = names(fit$model)[2], y = 
          names(fit$model)[1])) + 
            geom_point() +
            stat_smooth(method = "lm", col = "red") +
            labs(title = paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 
              5),
                   "Intercept =",signif(fit$coef[[1]],5 ),
                   " Slope =",signif(fit$coef[[2]], 5),
                   " P =",signif(summary(fit)$coef[2,4], 5)))

}

               taus <-c(0.05  , 0.25, 0.50 , 0.75, 0.90 , 0.95)

                m <- ggplotRegression( lm(formula = BMI ~   height_in_m 
                  +weight_in_kg+ Highest_Education_level + 
                   wealth_index + age_in_year_groups, data = dat_new))

             m+geom_quantile(quantiles=taus, lwd=1.5 , col="green4", 
             fill=taus)

Now I want to add specific colours for each quantiles and also add spcific legend for each quantiles .

解决方案

Many ggplot statistics let you use the results of the calculation enclosed in .., for example with geom_density you can use ..count.. in the aes.

With geom_quantile you can use ..quantile..

df <- data_frame(x = rnorm(100), y = rnorm(100))
ggplot(df, aes(x, y)) + 
  geom_point() + 
  geom_quantile(aes(colour = as.factor(..quantile..))) 

The trick is to find out what these variables are called. Geoms that need to calculate statistics, such as geom_quantile and geom_density, have an associated ggproto object such as StatQuantile and StatDensity which has the code for the calculations in an element called compute_group.

The last command of StatQuantile$compute_group is

    plyr::ldply(quantiles, quant_pred, data = data, method = method, 
    formula = formula, weight = weight, grid = grid, method.args = method.args)

The function here, quant_pred - which you can see with ggplot2:::quant_pred, returns a list. The components of this list, including quantile, can be used in the aes.

这篇关于如何在ggplot2的同一图中为不同的分位数添加不同的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆