I've been trying to plot a graph in R using the gap.barplot() function in plotrix to add a line break in the Y axis, but it was unsuccessful. I have gone through several questions here on stackoverflow but I couldn't find anything that worked for me. This is the dataframe and code I used -
Condition <- c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B")
Value <- c("32", "43", "33", "65", "78", "66", "3", "1.2", "0.8", "2", "1", "1.4")
data1 <- data.frame(Condition, Value)
Since "B" has very low values compared to "A", I wanted to introduce a line break between 44 and 64 as there are no y values within this range.
library("plotrix")
gap.barplot(data1$Value,gap=c(44, 64), ylim = c(0, 80))
I DO get a graph with individual data points on the X axis instead of 2 bars each for A and B, some missing values and an error -
Error in y[bigones] - gapsize : non-numeric argument to binary operator
I need a graph with "A" and "B" on the X axis with two bars and the jitter points of the respective data with a 'p value' and the line break as specified. Is this possible or am I being greedy? Can somebody tell me how this is done? Thank you.
... the graph must have the mean of A and B on separate bars with their respective data points as jitters and an error bar
You need to calculate the mean values for each group first. I add the size and sd as well to facilitate adding the jittered points and error bars later.
data2 <- aggregate(Value~Condition, FUN=\(x) c(mean=mean(x), n=length(x), sd=sd(x)), data=data1)
library(plotrix)
gap <- c(5,28)
opar <- par(las=1, mar=c(4,4,1,1))
gp <- gap.barplot(data2$Value[,1],
gap=gap,
ylim = c(0, 56),
xlim=c(1, 2.5),
xaxlab=data2$Condition,
xtics=c(1.5, 2),
ytics=c(0, 3, seq(30,80,10)),
ylab="Value", xlab="Condition")
gap.plot(x=jitter(rep(gp, times=c(data2$Value[,2]))),
y=data1$Value,
add=TRUE,
gap=gap, pch=19, col="red")
for(i in 1:2)
arrows(x0=gp[i], x1=gp[i],
y0=data2$Value[i,1] - ifelse(i==1, diff(gap), 0),
y1=data2$Value[i,1] + data2$Value[i,3] - ifelse(i==1, diff(gap), 0),
code=3, length=0.05, angle=90)
par(opar)