0

I have been trying to use geom_boxplot() instead of geom_point() in the following plot, but unfortunately, I have been getting errors.

Also, I want to keep the relevant ticks on the x-axis (i.e. remove 20 and 40 from the x-axis).

Finally, is it possible to include mathematical expressions in the title of the sub-plots?

Plot

My code is

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")

Your help is appreciated

stefan
  • 90,330
  • 6
  • 25
  • 51
MOHAMMED
  • 400
  • 1
  • 9
  • 1
    these answers might be helpful: https://stackoverflow.com/search?q=%5Br%5D+ggplot+boxplot+for+continuous+x, https://stackoverflow.com/search?q=%5Br%5D+ggplot+expression+in+facet+label, https://stackoverflow.com/search?q=%5Br%5D+ggplot+set+axis+breaks – I_O Jun 04 '23 at 06:07

1 Answers1

2

I'm not completely sure I understand what you want, but here is a suggestion:

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")

enter image description here

Ref here for how to add the expression to the facet title: Adding expressions to facet labeller in ggplot 2 2.0