I am not being able to display the multi-axis level for the x-axis. I have read the solution in StackOverflow and I am trying to implement it. However, I am not being able to display the text outside of the plot limiting the y axis.
library(reprex)
library(ggplot2)
value<-(sample(x=1:5, size = 96, replace = TRUE))
person_id<- rep(paste("P", 1:24), each = 4)
cluster_id<- rep(paste("C", 1:12), each=8)
df<- data.frame(person_id, cluster_id, value)
p<- ggplot(df, aes(x = interaction(person_id, cluster_id), y = value))+geom_point(size = 4)+ geom_boxplot()+
annotate(geom = "text", x = seq_len(24), y = -0.30, label = levels(df$person_id), size = 4) +
annotate(geom = "text", x = 1.5 + 2 * (0:11), y = -0.45, label = levels(df$cluster_id), size = 5)+theme(axis.line = element_line(colour = "black"),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.border = element_blank(),panel.background = element_blank(), axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())
p
[image]
# moving the annotate text outside the plot
p+coord_cartesian( ylim = c(1, 5), clip = "off")
[image]
# theme(plot.margin=unit(c(1,1,3,1.2),"cm"))