将大平均值添加为线和圆+在图例中添加条目

Posted

技术标签:

【中文标题】将大平均值添加为线和圆+在图例中添加条目【英文标题】:Add grand average as both line and circle + add the entry in the legend 【发布时间】:2017-12-13 22:27:00 【问题描述】:

我想在情节 1) 中添加每个条件的主题和重复次数的总平均值;和 2) 在图例上添加此项。

使用此代码(编辑:我制作了一个类似的 df 来解决我的问题):

Npoint = 12; Ncond = 7; Nrep = 3; Nsubj = 2;

data_a <- c(sample(c(-2,22), Nrep*Npoint, replace = TRUE), sample(c(38,60), Nrep*Npoint, replace = TRUE), 
        sample(c(46,62), Nrep*Npoint, replace = TRUE), sample(c(26,44), Nrep*Npoint, replace = TRUE),
        sample(c(42,58), Nrep*Npoint, replace = TRUE), sample(c(46,58), Nrep*Npoint, replace = TRUE),
        sample(c(38,56), Nrep*Npoint, replace = TRUE), sample(c(-2,22), Nrep*Npoint, replace = TRUE), 
        sample(c(38,60), Nrep*Npoint, replace = TRUE), sample(c(46,62), Nrep*Npoint, replace = TRUE), 
        sample(c(26,44), Nrep*Npoint, replace = TRUE), sample(c(42,58), Nrep*Npoint, replace = TRUE), 
        sample(c(46,58), Nrep*Npoint, replace = TRUE), sample(c(38,56), Nrep*Npoint, replace = TRUE))

subj_t <- rep(c("s01", "s02"), each=Npoint*Ncond*Nrep);
cond_t <- rep(c("Quiet","1","1.41","2","2.82","4","TEN"), each=Nrep*Npoint, times=Nsubj);
rep_t  <- rep(c("a","b","c"), each=Npoint, times=Ncond*Nsubj); 

RAC_QST_a <- data.frame(data=data_a, subj=factor(subj_t), cond=factor(cond_t), rep=factor(rep_t))

library(ggplot2); library(forcats);

ggplot(RAC_QST_a, aes(x=fct_inorder(cond), y=data, group=interaction(subj,rep), color=subj, shape=rep, na.rm=TRUE)) + 
theme_bw() +  
stat_summary(fun.y = "mean", geom = "point", size = 2.5) +
stat_summary(fun.data = mean_sdl, geom = "errorbar",  size = .25, linetype = "solid") +
stat_summary(fun.y = "mean", geom = "line", size = .25, linetype = "dashed")  + 
stat_summary(fun.y = "mean", aes(group = cond), geom = "point", size = 3, shape=1, colour="black") + 
scale_y_continuous(breaks = c(10,20,30,40,50,60), label = c(10,20,30,40,50,60)) + 
labs(title = "behavioural RAC", x = "Notch Central Freq [KHz]", y = "Threshold [dB SPL]")

我有这张图片,请看这里:http://imgur.com/LU8QJgm

理想情况下,我想获得:

那么,请问我怎样才能在圆圈之间添加黑线并在腿上添加条目?

我无法根据我的情况调整这个答案Add an entry to the legend for a manually added line……

提前谢谢你

【问题讨论】:

【参考方案1】:

我明白了,感谢 JoFrhwld 的回答 (ggplot2: line connecting the means of grouped data)

简单地说:

stat_summary(fun.y = "mean", aes(group = 1), linetype = "average"), 
           geom = "line", size = 1, shape=1, colour="black") +
scale_linetype_manual("", values = 3) +  guides(fill = guide_legend(override.aes = list(linetype = 1))) + 

【讨论】:

以上是关于将大平均值添加为线和圆+在图例中添加条目的主要内容,如果未能解决你的问题,请参考以下文章

将图例添加到组合的线和条形图 ggplot

如何将 ggplot 注释放在我的图例上?

如何向ggplot直方图添加均值和模式?

ggplot2 函数中 geom_boxplot 的平均值和中值箱线图图例

如何为 geom_tile 中的每个变量添加单独的图例?

echarts之series,markLinemarkPoint