ggplot个人密度与平均密度 [英] ggplot individual densities with mean density

查看:177
本文介绍了ggplot个人密度与平均密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个不同类别的密度绘制成线条,然后将每个类别的平均密度叠加为字母。下面是代码,以显示平均密度只是概括疾病和治疗。我怎样才能用代表每个人的密度的线条(最好是红色代表疾病= 1,蓝色代表疾病= 2)?即在每个疾病中一行,治疗组合如下:

 #以融化形式初始化数据框
#2疾病类别
#来自每种疾病的10人
#2给予每个人
#25的变量每人测量值/治疗组合
set.seed(6737334)$ b $ (2 * 10 * 2 * 25),var = rep(1:25,times = 2 * 10),
val = rnorm(2 * 10 * 2 * 25),
疾病= as.factor(rep(1:2,each = 2 * 10 * 25)),
treat = as.factor(rep(1:2,times = 10 ,每个= 25)))

plot <-ggplot(df,aes(x = val,fill = disease))+ geom_density(alpha = .3)+
facet_grid )。

print(plot)



########## ########编辑

这段代码非常接近我想要的解决方案。我怎样才能让线条变成黑色,与疾病类别的颜色相匹配?

 #以融化形式初始化数据框
#2疾病类别
#来自每种疾病的10人
#2给予每个人的治疗
#25具有每人测量值/治疗组合的变量
set.seed (6737334)
df < - data.frame(id = 1:(2 * 10 * 2 * 25),var = rep(1:25,times = 2 * 10),
test = as.factor(rep(1:(2 * 10),each = 25)),
val = rnorm(2 * 10 * 2 * 25),
疾病= as.factor(rep(1 :2,each = 2 * 10 * 25)),
treat = as.factor(rep(1:2,times = 10,each = 25)))

ggplot(df )+
geom_density(aes(x = val,group = test,color = disease))+
geom_density(aes(x = val,fill = disease),color = NA,alpha = .5) +
facet_grid(treat〜。)

这个方法证明是利用 group 选项。 c $ c> aes()交互。看下面的完整例子。感谢上面的@Jaap和此问题指向正确的方向。

 #以融化的形式初始化数据框
#2疾病类别
#来自每种疾病的10人
#2给予每个人
#25的变量每人测量值/治疗组合
set.seed(6737334)$ b $数据帧(id = 1:(2 * 10 * 2 * 25),var = rep(1:25,times = 2 * 10),
test = as.factor(rep 1:(2 * 10),每个= 25)),
val = rnorm(2 * 10 * 2 * 25),
疾病= as.factor(rep(1:2,each = 2 * 10 * 25)),
treat = as.factor(rep(1:2,times = 10,each = 25)))

ggplot(df)+
geom_density(aes(x = val,fill =疾病),alpha = 0.5)+
(aes(x = val,group = interaction(test,disease),color = disease))+
geom_density导游( fill = guide_legend(title =mean))+#else legend title'NA'
facet_grid(treat〜。)


I would like to plot densities from two different categories as lines and then the mean density of each category overlaid as alpha. Below is code to show the 'mean' densities only summarised over disease and treatment. How can I overlay this with lines representing the density for each individual (preferably red for disease = 1 and blue for disease = 2)? i.e. a line per disease, treatment combination in the example:

# Initialise data frame in melted form
    # 2 disease categories
    # 10 people from each disease
    # 2 treatments given to each individual
    # 25 variables with measured values per person/treatment combination
set.seed(6737334)
df <- data.frame(id = 1:(2*10*2*25), var = rep(1:25, times=2*10), 
                 val = rnorm(2*10*2*25),
                 disease = as.factor(rep(1:2, each=2*10*25)),
                 treat = as.factor(rep(1:2, times=10, each=25)))

plot <- ggplot(df, aes(x=val, fill=disease)) + geom_density(alpha=.3) +
    facet_grid(treat ~ .)

print(plot)

################## EDIT

This code is very very close to my desired solution. How can I make the lines, currently black, match the colour of the disease category?

# Initialise data frame in melted form
    # 2 disease categories
    # 10 people from each disease
    # 2 treatments given to each individual
    # 25 variables with measured values per person/treatment combination
set.seed(6737334)
df <- data.frame(id = 1:(2*10*2*25), var = rep(1:25, times=2*10),
                 test = as.factor(rep(1:(2*10), each=25)),
                 val = rnorm(2*10*2*25),
                 disease = as.factor(rep(1:2, each=2*10*25)),
                 treat = as.factor(rep(1:2, times=10, each=25)))

ggplot(df) + 
    geom_density(aes(x=val, group=test, color=disease)) +
    geom_density(aes(x=val, fill=disease), color=NA, alpha=.5) +
    facet_grid(treat ~ .)

解决方案

The method turned out to be to utilise the group option of aes() with interaction. See full example below. Thank you to @Jaap above and this question for pointing in the right direction.

# Initialise data frame in melted form
    # 2 disease categories
    # 10 people from each disease
    # 2 treatments given to each individual
    # 25 variables with measured values per person/treatment combination
set.seed(6737334)
df <- data.frame(id = 1:(2*10*2*25), var = rep(1:25, times=2*10),
                 test = as.factor(rep(1:(2*10), each=25)),
                 val = rnorm(2*10*2*25),
                 disease = as.factor(rep(1:2, each=2*10*25)),
                 treat = as.factor(rep(1:2, times=10, each=25)))

ggplot(df) + 
    geom_density(aes(x=val, group=interaction(test,disease), colour=disease)) +
    geom_density(aes(x=val, fill=disease), alpha=.5) +
    guides(fill=guide_legend(title="mean")) +     # else legend title 'NA'
    facet_grid(treat ~ .)

这篇关于ggplot个人密度与平均密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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