R语言 ggplot2:将geom_poin替换为geom_boxplot

4jb9z9bj  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(134)

我一直在尝试使用geom_boxplot()而不是geom_point()在下面的图,但不幸的是,我一直得到错误。
此外,我想保留x轴上的相关刻度(即从x轴移除20和40)。
最后,是否有可能在子情节的标题中包含数学表达式?

我的准则是

  1. n=48
  2. p=7
  3. df=data.frame(BIAS = c(rnorm(n*p,2,.01),rnorm((n+10-2),4),rnorm(n*p,1.5,.01),rnorm((n+30-2),1),rnorm(n*p,1,.01),rnorm((n+50-2),0)),
  4. ID = rep(c(10,30,50),c((n*p+(n+10-2)),(n*p+(n+30-2)),(n*p+(n+50-2)))),
  5. TYPE = rep(c("DELTA","PSI","DELTA","PSI","DELTA","PSI"),c(n*p,(n+10-2),n*p,(n+30-2),n*p,(n+50-2))))
  6. ggplot(df,aes(x=ID ,y=BIAS, color=TYPE)) +
  7. geom_point() +
  8. facet_wrap(~TYPE,scales = "free")+
  9. theme_bw() + xlab("T") + ggtitle("N = 48")

感谢你的帮助

bq3bfh9z

bq3bfh9z1#

我不太清楚你想要什么,但我有一个建议:

  1. n=48
  2. p=7
  3. df=data.frame(BIAS = c(rnorm(n*p,2,.01),rnorm((n+10-2),4),rnorm(n*p,1.5,.01),rnorm((n+30-2),1),rnorm(n*p,1,.01),rnorm((n+50-2),0)),
  4. ID = rep(c(10,30,50),c((n*p+(n+10-2)),(n*p+(n+30-2)),(n*p+(n+50-2)))),
  5. TYPE = rep(c("DELTA","PSI","DELTA","PSI","DELTA","PSI"),c(n*p,(n+10-2),n*p,(n+30-2),n*p,(n+50-2))))
  6. #Change ID to factor
  7. df$ID <- as.factor(df$ID)
  8. #Label TYPE = PSI as an expression
  9. df$TYPE <- factor(df$TYPE,
  10. labels=c('DELTA','{}^0*italic(D)~plain((rarefied))'))
  11. ggplot(df,aes(x = ID, y=BIAS, color = ID)) +
  12. geom_boxplot() +
  13. #labeller = label_parsed added so that the expression is evaluated correctly
  14. facet_wrap(~TYPE,scales = "free", labeller = label_parsed) +
  15. theme_bw() +
  16. xlab("T") +
  17. ggtitle("N = 48")

有关如何将表达式添加到facet标题的信息,请参考此处:Adding expressions to facet labeller in ggplot 2 2.0

展开查看全部

相关问题