R语言 ggplot2:将geom_poin替换为geom_boxplot

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

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

我的准则是

n=48
p=7
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)),
              ID   = rep(c(10,30,50),c((n*p+(n+10-2)),(n*p+(n+30-2)),(n*p+(n+50-2)))),
              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))))

ggplot(df,aes(x=ID ,y=BIAS, color=TYPE)) + 
  geom_point() +
  facet_wrap(~TYPE,scales = "free")+
  theme_bw() + xlab("T") + ggtitle("N = 48")

感谢你的帮助

bq3bfh9z

bq3bfh9z1#

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

n=48
p=7
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)),
              ID   = rep(c(10,30,50),c((n*p+(n+10-2)),(n*p+(n+30-2)),(n*p+(n+50-2)))),
              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))))
#Change ID to factor
df$ID <- as.factor(df$ID)
#Label TYPE = PSI as an expression
df$TYPE <- factor(df$TYPE,
                        labels=c('DELTA','{}^0*italic(D)~plain((rarefied))'))

ggplot(df,aes(x = ID, y=BIAS, color = ID)) + 
  geom_boxplot() +
  #labeller = label_parsed added so that the expression is evaluated correctly
  facet_wrap(~TYPE,scales = "free", labeller = label_parsed) +
  theme_bw() + 
  xlab("T") + 
  ggtitle("N = 48")

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

相关问题