Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 add text on top of boxplots

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

like image 844
Rad Avatar asked Apr 15 '14 23:04

Rad


People also ask

How do you add text to a boxplot?

Firstly, to create boxplot we'll use the seaborn library, and to display text on the boxplot we will be using the text() method available inside the matplotlib. pyplot class. The text() method enables us to write strings on the plots and add customizations to them.

How do I add text to ggplot2?

You can use the annotate() function to add text to plots in ggplot2. where: x, y: The (x, y) coordinates where the text should be placed. label: The text to display.

How do I add a label to a boxplot in R?

We can add labels using the xlab,ylab parameters in the boxplot() function. By using the main parameter, we can add heading to the plot. Notch parameter is used to make the plot more understandable.

Which code is used to create boxplot in ggplot2?

In ggplot2, geom_boxplot() is used to create a boxplot.


1 Answers

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)

enter image description here

like image 198
tonytonov Avatar answered Oct 06 '22 18:10

tonytonov