如何更改箱线图中 x 值的名称

How to change the names of the x values in a boxplot

对不起,如果这是非常基础的,但我对此很陌生。我正在使用 boxplot() 在 R 中为一些调查数据创建箱线图。我注意到它会自动按字母顺序排列 x 组,这不符合我的需要。在我的数据框中,我将名称更改为开头有 'a' 或 'b'。这使它们按我想要的顺序排列,但标签不整齐。我希望能够用我自己的标签覆盖数据框中的 x 标签,或者决定值沿 x 轴的顺序。基本上我只想先 'Riparian',然后 'Floodplain'。我还有许多其他箱线图要做,有些箱线图在 x 轴上会有超过 2 个值。

这是没有尝试覆盖标签的数据和代码

data

脚本

boxplot(tot_sp ~ hab, data = mydata, xlab= "Habitat Type", ylab = "Total # Species") 
title("Bird Search: Total Species")

箱线图

我试过的其他一些脚本,none 有效。

boxplot(tot_sp ~ hab, data = mydata, axis(1, at=seq(1, 2), labels = labels.name), 
        xlab= "Habitat Type", ylab = "Total # Species") 

boxplot(tot_sp ~ hab, data = mydata, 'labels',{'Riparian', 'Floodplain'}, at=NULL, 
        xlab= "Habitat Type", ylab = "Total # Species") 

boxplot(tot_sp ~ hab, data = mydata, xtix = {'Riparian', 'Floodplain'}, xtixloc = [1, 2], 
        xlab= "Habitat Type", ylab = "Total # Species") 

非常感谢您的帮助!

这是暗中刺杀:

mydata$hab <- factor(mydata$hab, levels=c("Riparian", "Floodplain")

boxplot(tot_sp ~ hab, data = mydata,
        xlab= "Habitat Type", ylab = "Total # Species") 

使用 iris 数据集重新排序的示例:

df <- iris
df$Species <- factor(df$Species, levels=c("virginica", "setosa", "versicolor"))
boxplot(Sepal.Length ~ Species, df)