Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

r - Add text to individual facets in ggplot

I wonder how I put different text in individual facets. I want to add the text inside the plot using annotate or geom_text() I know there are duplicated posts in this, but I don't manage to get it correct. Here is my data:

Experiment         FC           Pairing
Meister et al. -2.74236520      yes
Meister et al. -0.7436354       no
Meister et al. -2.74236520      yes
Meister et al. -0.73536354      no
daub et al.    -0.64246768      yes
daub et al.    -0.6663321       no
daub et al.    -0.64246768      yes
daub et al.    -0.6663321       no
hans et al.    -2.32230716      yes
hans et al.    -0.49423279      no
hans et al.    -2.32723716      yes
hans et al.    -0.4944279       no

ggplot(combined_pos1,aes(Pairing,FC,fill=as.factor(Pairing))) + 
      geom_boxplot(fill = "grey90") +  coord_cartesian(ylim=c(-3,3)) + 
      facet_grid(~Experiment)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

create the annotations for each facet (here as an example for Meister and Hans):

combined_pos1$annotations = c("Text for Meister",rep("",10),"Text for hans")

include geom_text:

g = ggplot(combined_pos1,aes(Pairing,FC,fill=as.factor(Pairing))) + geom_boxplot(fill = "grey90") +  coord_cartesian(ylim=c(-3,3)) + facet_grid(~Experiment)
g = g + geom_text(aes(x=2.5,y=2.5,label=annotations))
g

This yields the following: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...