ggplot2在箱形图上添加文本 [英] ggplot2 add text on top of boxplots

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

问题描述

我有一个数据,我正在将 ggplot2 作为箱形图绘制,看起来像

 >头(varf)
sID变量值
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)

我有一个带注释的数据框

 类别注释
CM001 HG4450
CM002 HG3288
..
CM016 MM8998

我想将这些注释映射到我的箱子图上,但是找不到这样做的方法,在boxplot中使用geom_text的正确语法是什么?



谢谢

解决方案

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

pre code> 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)


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

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

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 ?

Thanks

解决方案

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天全站免登陆