如何在间隙上方的gap.boxplot()中添加刻度线和刻度线以及线段和轴尺寸的问题

问题描述 投票:0回答:1

这是我的数据集:

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))

使用这些数据,我制作了一个箱线图,但将框设置为白色(不可见),并使用segment()将数据作为线添加到图表中:

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")

这创建了这个图表:

可悲的是,这里出现了一些问题,我不知道如何修复它们:

  1. 刻度线和相应的数字不会显示在间隙上方的 y 轴上。
  2. 输出中看不到轴标签的大小 (cex=2.8)。
  3. 我为其创建间隙的 data_N3[,3] 的数据点(段)没有显示在间隙上方的图表中。

任何有关这些问题的帮助将不胜感激:)

r boxplot plotrix
1个回答
0
投票

使用gap.boxplot函数时出现以下错误:

gap.boxplot(data_N3,axes = FALSE,plot = F,family =“Times”,中的错误: 差距不能包括中位数、四分位数或订书钉

这基本上是告诉您不能在箱线图的中间休息。 一般来说,错误不应被忽视。

这可能也与您丢失更高值的点的问题有关。 如果你想绘制点,你可以考虑使用基本图或 ggplot 作为初始图(这样你就不会收到错误)——请参阅这篇文章的 ggplot 示例: 强制 y 轴从 0 开始,插入“break”,并使用 ggplot2 有一个大的 y 轴

对于#2,如果将 cex 更改为 cex.axis,它将更改轴字体的大小。

© www.soinside.com 2019 - 2024. All rights reserved.