0

I am trying to label my facets in a facet grid plot using Greek character equalities, with a line break in between the two equalities.

The code I have used to generate this is the following:

set.seed(1)  
outtown<-cbind(rep(1,100),runif(100,-100,100),runif(100,-100,100),rep(1,100))
colnames(outtown)<-c('hh_id','x','y','z')
outtown[,1]<-print(paste("alpha==",100,"\n beta==",200,sep=""))  
outtown<-data.frame(outtown) 
outtown[,1]<-as.factor(outtown[,1])  
outtown[,2]<-as.numeric(as.character(outtown[,2]))  
outtown[,3]<-as.numeric(as.character(outtown[,3]))  
outtown[,4]<-as.factor(outtown[,4])  
str(outtown)

library(ggplot2) 
ev <- ggplot(outtown, aes(x = outtown[,2], y = outtown[,3])) +
  geom_boxplot(mapping=aes(x=outtown[,2],y=outtown[,3],shape=1,colour=outtown[,1]),outlier.size = 0.5,lwd=0.3) +
  scale_shape_identity() +
  theme(axis.text.x=element_text(angle = 45, hjust = 1,size=5))

ev + facet_grid(z~hh_id,scale='free',labeller=label_parsed)+
  xlab("Estimator") +
  ylab("Bias") +
  scale_color_discrete(name='Type of\nEstimator')

Although no error comes out, I get an image just showing the "alpha=100" part, but not another line underneath saying "beta=200". Here is the image:Disregard the legend or the labels

Aside from the labels, how do I get it to say on the top facet "alpha=100" with "beta=200" underneath? Thank you

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
tinder
  • 35
  • 7

1 Answers1

0

I had a similar question and found that the answer from @plannapus here:

Combining `expression()` with `\n`

using atop() could be adapted to solve the question.

set.seed(1)  

outtown <- data.frame(hh_id = rep(1,100), 
           x = runif(100,-100,100), 
           y = runif(100,-100,100), 
           z  = rep(1,100))

outtown$hh_id <- "atop(alpha==100, beta==200)"

library(ggplot2) 
ev <- ggplot(outtown, aes(x = x, y = y)) +
  geom_boxplot(mapping=aes(x=x,y=y,shape=1, group = hh_id),outlier.size = 0.5,lwd=0.3) +
  scale_shape_identity() +
  theme(axis.text.x=element_text(angle = 45, hjust = 1,size=5))

ev + facet_grid(z~hh_id,scale='free',labeller=label_parsed)+
  xlab("Estimator") +
  ylab("Bias") 

3RK
  • 5
  • 4