BJPrim
BJPrim

Reputation: 358

R: Boxplot names overlap with x axis

I have a probably very easy question but I just can't find a proper solution for it. I have the following code:

    data <- as.data.frame(matrix(rnorm(30),15,2))
    names(data) <- c("BoxplotData1","BoxplotData2")
    boxplot(data, names = c("Box \n Plot \n Data1","Box \n Plot \n Data2"))
    axis(1, at=1:2,labels = FALSE)

which gives me an output as shown in the plot below. I would now like to change the position of the boxplot names so that there is no overlapping anymore. I just find ways to increase the distance between axis title and the names, but I just don't find a way to solve my issue.

Thank you for any comment! enter image description here

Upvotes: 2

Views: 2836

Answers (2)

Richard Telford
Richard Telford

Reputation: 9923

You can change the graphical parameters in par

par(mgp = c(3, 3,0))#mgp sets position of axis label, tick labels and axis
boxplot(data, names = c("Box \n Plot \n Data1","Box \n Plot \n Data2"))

If your labels are very long, you might also need to set mar to increase margin size.

Upvotes: 3

Spacedman
Spacedman

Reputation: 94202

Do the boxplot with no names, add them using axis with the line parameter to give them some space. Use lwd=0 to suppress redrawing of the axis lines and ticks:

boxplot(data,names=c("",""))
axis(1,at=1:2, c("Line1\nLine2\nLine3","Box\nPlot\nLine3"),
  line=2, lwd=0)

Upvotes: 2

Related Questions