How to add ticks and tickmarsk in gap.boxplot() above the gap and problem with segments and axis size

30 views Asked by At

Here is my dataset:

data_N3=structure(c(2.7240367196496, 4.36841055897346, 2.71177214885543, 
4.36849175181861, 17.7213767231357, 17.626304994383, 17.6468190136497, 
17.6321641647487, 1121.5558117271, 1116.19429181322, 1116.20063832111, 
1116.23541006948, NA, NA, NA, NA, 15.5774104850416, 5.54498649819063, 
15.4691309692694, 5.54438655223586, 11.0853210461256, 7.34658260987168, 
25.2590667564114, 12.1012005413784, 17.5713360077048, 8.44869502735733, 
17.4844139957775, 8.44912288685442), .Dim = c(4L, 7L))

With this data I make a boxplot, but make the boxes white (invisible) and add the data as lines to the graph using segments():

library("plotrix")
data_N3[,4] <- 0 #gap.boxplot cannot contain NA values
par(mgp=c(4,2.3, 0), mar=c(10, 8, 2, 2), family="Times")

gap.boxplot(data_N3, axes=FALSE,
            col='white', family="Times", 
            whisklty = 0, staplelty = 0, medcol="white", xlab="", ylab="",
            border = 'white',
            gap = list(top=c(30,1110), bottom=c(NA,NA)) 
)

label=c("Control", 1, 2, 3, 1, 2, 3)
axis(1, c(1,2,3,4,5,6,7), labels=label, cex=2.8)
axis(2, c(10, 20, 30, 1110, 1120), cex=2.8)

par(family="Times")

#x-axis lines and cross
abline(v=4.5, lwd=4)
abline(v=1.5, lwd=4)
points(x=4, y=3.3, pch=4, cex=8, lwd=15, col='#238b45')

#x-axis texts
mtext(c("Plants with aphids", "Floral resources"), side=1, at=c(3.1, 6), cex=3, line=5)
mtext("Resource removed", side=1, cex=3, line=7.8, at=3.5)
mtext(expression(paste("Population density " (number/m^{2}))), side=2, cex=3, line=4.2)


#Visualising averages of years
n=ncol(data_N3)
x0s=1:n-0.38  #fitting lines between boxplot width
x1s=1:n+0.38

#Adding the data as segments so I get lines instead of boxplots
data_N3[,4] <- NA
y0s1=data_N3[1,]
y0s2=data_N3[2,]
y0s3=data_N3[3,]
y0s4=data_N3[4,]
segments(x0=x0s, x1=x1s, y0=y0s1, lwd=8, col='#238b45')
segments(x0=x0s, x1=x1s, y0=y0s2, lwd=8, col='#238b45')
segments(x0=x0s, x1=x1s, y0=y0s3, lwd=8, col='#238b45')
segments(x0=x0s, x1=x1s, y0=y0s4, lwd=8, col='#238b45')

#Red line showing the mean densities when no sub-habitats are removed
abline(h=3.543178, lwd=6, col="red")

Which creates this graph: enter image description here

Sadly, a couple of things go wrong here and I don't know how to fix them:

  1. The tick marks and the corresponding numbers do not show up on the y-axis above the gap.
  2. The size (cex=2.8) of the axis labels are not seen in the output.
  3. The data points (segments) of data_N3[,3] for which I made the gap does not show up in the graph above the gap.

Any help on any of these questions would be highly appreciated :)

1

There are 1 answers

1
hoganhaben On

I get the following error when using the gap.boxplot function:

Error in gap.boxplot(data_N3, axes = FALSE, plot = F, family = "Times", : gap cannot include the median, interquartiles or the staples

which is basically telling you that you cannot put a break in the middle of the boxplot. Generally, errors should not be ignored.

This is probably related to your issue of missing points for higher values also. If you want to plot points, you might consider using base plot or ggplot for the initial plot (so you dont get an error) -- see this post for a ggplot example: Force y axis to start at 0, insert "break", AND have a large y axis using ggplot2

for #2, if you change cex to cex.axis it will change the size of your axis fonts.