添加facet_grid面板均值作为文字和线条 [英] Add facet_grid panel means as text and hline

查看:88
本文介绍了添加facet_grid面板均值作为文字和线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框.

> head(df)
  DGene JGene cdr3_len Sum
1 IGHD1 IGHJ1        0  22
2 IGHD1 IGHJ1        1  11
3 IGHD1 IGHJ1        2  16
4 IGHD1 IGHJ1        3  40
5 IGHD1 IGHJ1        4  18
6 IGHD1 IGHJ1        5  30
...

facet_grid非常简单.

ggplot(df,aes(x=cdr3_len,y=Sum)) + geom_line() + xlim(c(1,42)) + facet_grid(JGene~DGene,scales="free_y")

并得到类似的东西.

我想知道是否有人可以帮我在每个网格的均值上添加一条线.或者可能是如何在右上角打印每个网格的均值.

谢谢

编辑- 完整链接到数据框

解决方案

这是一种通过预先计算所需值(根据@ jwillis0720的注释)同时为cdr3_len的平均值添加文本和垂直线的方法: /p>

首先,为每个面板计算cdr3_len的平均值,然后将该数据框left_join转换为第二个数据框,该数据框计算用于将文本放置在每个面板上的适当y值(因为适当的y值会有所不同仅按JGene级别).

library(dplyr) 

meanData = df %>% group_by(JGene, DGene) %>%
  summarise(meanCDR = sum(Sum*cdr3_len)/sum(Sum)) %>%
  left_join(df %>% group_by(JGene) %>%
              summarise(ypos = 0.9*max(Sum)))

现在该情节:

ggplot(df,aes(x=cdr3_len, y=Sum)) +
  geom_vline(data=meanData, aes(xintercept=meanCDR), colour="red", lty=3) +
  geom_line() +
  geom_text(data=meanData, 
            aes(label=round(meanCDR,1), x=40, y=ypos), colour="red",
            hjust=1) +
  xlim(c(1,42)) + 
  facet_grid(JGene~DGene,scales="free_y")

I have a dataframe that looks like this.

> head(df)
  DGene JGene cdr3_len Sum
1 IGHD1 IGHJ1        0  22
2 IGHD1 IGHJ1        1  11
3 IGHD1 IGHJ1        2  16
4 IGHD1 IGHJ1        3  40
5 IGHD1 IGHJ1        4  18
6 IGHD1 IGHJ1        5  30
...

It is pretty simple to facet_grid.

ggplot(df,aes(x=cdr3_len,y=Sum)) + geom_line() + xlim(c(1,42)) + facet_grid(JGene~DGene,scales="free_y")

and getting something that looks like.

I was wondering if anyone could help me with adding a hline to the mean of each grid. Or possibly how to print the mean of each grid in the top right corner.

Thanks,

Edit - Full link to dataframe

解决方案

Here's a way to add both text and a vertical line for the mean of cdr3_len by pre-computing the desired values (per @jwillis0720's comment):

First, calculate the mean of cdr3_len for each panel and then left_join that data frame to a second data frame that calculates the appropriate y-value for placing the text on each panel (because the appropriate y-value varies only by level of JGene).

library(dplyr) 

meanData = df %>% group_by(JGene, DGene) %>%
  summarise(meanCDR = sum(Sum*cdr3_len)/sum(Sum)) %>%
  left_join(df %>% group_by(JGene) %>%
              summarise(ypos = 0.9*max(Sum)))

Now for the plot:

ggplot(df,aes(x=cdr3_len, y=Sum)) +
  geom_vline(data=meanData, aes(xintercept=meanCDR), colour="red", lty=3) +
  geom_line() +
  geom_text(data=meanData, 
            aes(label=round(meanCDR,1), x=40, y=ypos), colour="red",
            hjust=1) +
  xlim(c(1,42)) + 
  facet_grid(JGene~DGene,scales="free_y")

这篇关于添加facet_grid面板均值作为文字和线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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