1

I have the following data:

fixed <- as.data.frame(rep(0.125,8))
fixed[,2] <- c("1","2","3","4","5","6","7","8")
colnames(fixed) <- c("Percentage", "Test")

and produced the following graph:

enter image description here

Now I would like to achive something like this. However, I have not found a way to draw outside of the graph area and write separate axis labels. Is that possible?

enter image description here

The code for my plot is here:

p <- ggplot(fixed,aes(x=Test ,y=Percentage,fill=Test))+
  geom_boxplot()+ 
  stat_summary(fun.data = function(y) 
    data.frame(y=0.6, label = paste(round(mean(y),2))), geom="text",size=3) +
  geom_hline(yintercept=0.55, linetype="dashed", color = "black")+
  theme(legend.position="none")+
  scale_y_continuous(limits = c(0, 0.6),breaks=seq(0,0.6,0.1),
                     labels= c("0%","10%","20%","30%","40%","50%","Mean"))+
  theme_bw()+
  labs(title = "Title")+
  xlab("Test")+
  ylab("Percetage")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = "grey90", colour = NA),
        plot.background = element_rect(fill = "grey90", colour = NA),
        legend.position="none",plot.title = element_text(size= 12, hjust=0.5),
        plot.margin = unit(c(1,1,1,1), "cm"))+
  scale_fill_manual(values=c("#00441b","#006d2c","#238b45","#41ab5d","#74c476",
                             "#a1d99b","#c7e9c0","#e5f5e0"))
p
Mucteam
  • 345
  • 3
  • 12
  • Using `ggplot2::annotate` then supply negative y-values to position the text below the x-axis, x-values would remain the same. Same approach can be applied to `geom_segment` then for the line splitting the centre. – NColl Dec 03 '18 at 16:55
  • 2
    look into the cowplot package. It allows you to draw all over the entire area of a ggplot plot. – iod Dec 03 '18 at 16:56
  • @NColl geom_segment only works if I take scale_y_continuous() away. But then it just draws the line within the graph area. How can avoid this? – Mucteam Dec 03 '18 at 17:19
  • Maybe something along the lines as answers to [this question](https://stackoverflow.com/questions/18165863/multirow-axis-labels-with-nested-grouping-variables)? – aosmith Dec 03 '18 at 18:56
  • 1
    Found the problem. Needed to take the limits out of scale_y_continuous() and add coord_cartesian(, clip="off") – Mucteam Dec 03 '18 at 20:33

0 Answers0