ggplot2 在箱线图顶部添加文本 [英] ggplot2 add text on top of boxplots

查看:38
本文介绍了ggplot2 在箱线图顶部添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据,我在 ggplot2 上绘制为箱线图,看起来像

I have a data that I'm plotting on ggplot2 as boxplots which look like

> head(varf)
             sID variable       value
1 SP_SA036,SA040   CM0001 0.492537313
2 SP_SA036,SA040   CM0001 0.479564033
3 SP_SA036,SA040   CM0001 0.559139785
4 SP_SA036,SA040   CM0001 0.526806527
5 SP_SA036,SA040   CM0001 0.009049774
6 SP_SA036,SA040   CM0001 0.451612903

变量列包含16个不同的ID(从CM0001到CM0016)

The variable column contains 16 different IDs (from CM0001 to CM0016)

我有一个带有注释的数据框

I have a dataframe with annotation

category   annotation
CM001      HG4450
CM002      HG3288
..
CM016      MM8998

我想将这些注释映射到我的箱线图之上,但找不到方法,将 geom_text 与箱线图一起使用的正确语法是什么?

I would like to map these annotations on top of my boxplots but couldn't find a way to do it, what is the right syntax of using geom_text with boxplot ?

谢谢

推荐答案

有很多方法可以解决这个问题,例如这里这里.可能最简单的方法是

There are many ways to approach this problem, e.g. here and here. Probably the simplest way is

meds <- c(by(mtcars$mpg, mtcars$cyl, median))
ggplot(mtcars, aes(factor(cyl), mpg)) +
    geom_boxplot() + 
    geom_text(data=data.frame(), aes(x=names(meds), y=meds, label=1:3), col='red', size=10)

这篇关于ggplot2 在箱线图顶部添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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