Add annotation of count of points greater or lower

2019-08-19 08:57发布

all!

I have managed to produce a plot like below using ggplot2 which have the total number of points in each group. If I want to add the number of points greater or lower than 0 separately, how can that be done?

Thank you!!!

give.n <- function(x){ 
  return(c(y = 10, label = length(x)))
  }


ggplot(dta, aes(x=type, y=foldChange, fill=grp)) +       
  geom_point(size=1,alpha=0.2,position = position_jitterdodge(jitter.width = .2),aes(col=grp)) + 
  geom_boxplot(alpha=0,outlier.shape=NA) + guides(fill=FALSE) + theme_bw() + xlab("") +
  geom_hline(yintercept = 0,col="red") +
  stat_summary(fun.data = give.n, geom = "text", fun.y = median, position = position_dodge(width = 0.75))

boxplots with geom_points

1条回答
Fickle 薄情
2楼-- · 2019-08-19 09:36

You can subset inside ggplot functions in order to plot whatever you want. In your case it would look like this if you wanted values > 0

ggplot(subset(dta, foldChange > 0), aes(x=type, y=foldChange, fill=grp))

I'm not sure what you mean exactly by adding the points later, but this should at least filter the points you desire.

查看更多
登录 后发表回答