使用geom_vline在多个面中的密度图中插入平均线 [英] Insert mean line in density plot for multiple facets using geom_vline

查看:109
本文介绍了使用geom_vline在多个面中的密度图中插入平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的数据集,我想使用密度图比较以下变量的两个性别的分布:撤回,影响和压力.这些图还应包含垂直线,以描绘每个性别的变量的平均值,并在同一图形上绘制不同的面.

For the dataset below, I would like to use a density plot to compare distributions in the two sexes for the following variables: withdraw, affect and estress . The plots should also contain vertical lines depicting the mean of the variables for each sex, and drawn on the same figure with different facets.

我当前的代码如下,但是我不知道如何使用geom_vline来获取每个方面的均值:

My current code is as follows, however I have no idea how to use geom_vline to get the mean for each facet:

vars_2_plot <- names(estress.dat1)[!names(estress.dat1) %in% "sex"]
estress.dat1 %>%
  pivot_longer(cols = all_of(vars_2_plot),
    names_to = "Predictors", values_to = "Values"
    ) %>%
  ggplot( aes(x = Values, fill = factor(sex) )) +   
  geom_density(alpha = 0.3) +
  facet_wrap(~ Predictors, scales = "free") +
  theme_minimal() +
labs(fill = "sex", x = "Predictor values") 

我的数据:

structure(list(withdraw = c(3, 1, 3.66, 4.66, 4.33, 3, 1, 1, 
2, 4, 4.33, 1, 5, 1.66, 4, 1.33, 1.66, 1.66, 2, 4, 2, 3.66, 3.66, 
4, 1, 1.66, 4.66, 5, 2.33, 1.33, 1.66, 1.33, 4, 1.66, 3, 1, 2, 
2, 2.66, 3, 1, 1, 4.66, 1, 1, 2, 1.33, 1, 3.66, 2.33, 1, 2, 2.33, 
2.33, 2.66, 2.33, 3, 1.66, 1, 2.66, 5, 3.33, 1, 2.33, 1, 1.33, 
3.33, 5, 2, 1, 2.33, 3, 1.66, 1, 1, 2, 1, 1, 4.33, 2, 1, 3, 1, 
2.66, 3.33, 2.33, 1, 1, 3, 2.66, 1.33, 2, 1, 1, 1, 2, 3, 1.33, 
2.33, 4.66, 2, 2.66, 1, 1, 4, 4, 2, 3, 1, 3, 1, 2.33, 1, 1, 2, 
2.33, 3, 1, 3, 1, 4.33, 3.33, 1, 1.33, 3.33, 1.66, 2, 1, 1, 2.33, 
4, 3, 2.33, 4, 4, 1, 1.33, 1, 1, 1.66, 2, 2.66, 3.66, 3.33, 1, 
2.33, 2.33, 1, 1, 1, 4.66, 2.33, 3, 3.33, 4.33, 1.33, 1, 3.66, 
4.33, 1, 2.66, 6.33, 2.66, 1, 3.33, 4.33, 1, 1.33, 1, 1, 3, 1, 
4, 1.33, 1, 2, 1.66, 1, 4, 1.66, 1, 1, 1.66, 5, 2.33, 1.66, 3, 
2.66, 1, 3.33, 4, 3, 2.66, 1, 3.66, 3, 2, 4, 7, 2, 1, 3.66, 2.66, 
1, 1.66, 1, 1.33, 3, 4, 4.66, 4, 2.66, 1, 1, 1, 1.66, 2.33, 3, 
3, 1, 4, 2.33, 4.33, 2, 2.66, 2, 1, 1, 1.66, 1, 1, 1.33, 2.33, 
1, 4, 2, 1, 2, 3, 3.66, 3, 3.66, 4, 3.66, 1, 1, 1, 2, 1, 2.66, 
1, 4, 1, 2.33, 4.33, 1.66, 2.66, 3.33, 3, 1, 2.33, 4), affect = c(2.6, 
1, 2.4, 1.16, 1, 1.5, 1, 1.16, 1.33, 3, 3, 2, 1.83, 1.16, 1.16, 
1, 1.33, 1.5, 1.33, 3, 3.16, 1.5, 1.5, 1.83, 1, 1.33, 1.5, 1.33, 
2.16, 1.16, 2.16, 1.83, 1.4, 1, 1.33, 1, 2.33, 1.16, 1, 1, 1.16, 
1.5, 2.16, 1.5, 2.66, 1.5, 1.16, 1.83, 2.66, 1.33, 1, 1.33, 1.83, 
1, 1.5, 1, 1.33, 1.33, 1.33, 1.16, 3, 1, 1, 1.66, 1.5, 1, 1.83, 
3.66, 3, 1.66, 1.83, 1.5, 1.66, 1, 1.16, 2.16, 1.16, 1, 1.5, 
1, 1.5, 4.33, 1.16, 2.33, 3, 2.33, 1.16, 1, 1.33, 1, 1.33, 1, 
1.16, 1, 1, 1.66, 1.5, 1.66,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 
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L
), .Label = c("0", "1"), class = "factor")), row.names = c(NA, 
-262L), class = "data.frame")

推荐答案

对于 geom_vline 中的 data = 参数,您需要一个数据栏,该栏的列必须与构面相同变量,以及您可以使用 colmeans 计算的相应平均值.

For the data= argument in geom_vline you need a data frame with a column identical to your facet variable, as well as the corresponding mean values which you may calculate using colmeans.

library(magrittr);library(tidyr);library(ggplot2)
vars_2_plot <- names(estress.dat1)[!names(estress.dat1) %in% "sex"]

estress.dat1 %>%
  pivot_longer(cols=all_of(vars_2_plot),
               names_to="Predictors", values_to="Values"
  ) %>%
  ggplot(aes(x=Values, fill=factor(sex) )) +   
  geom_density(alpha=0.3) +
  geom_vline(data=data.frame(means=colMeans(estress.dat1[-4]),
                             Predictors=colnames(estress.dat1[-4])), 
             aes(xintercept=means), color="red") +
  facet_wrap(~ Predictors, scales="free") +
  theme_minimal() +
  labs(fill="sex", x="Predictor values")

结果

这篇关于使用geom_vline在多个面中的密度图中插入平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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