简体   繁体   English

在 R 中为箱线图添加名称

[英]Add name to boxplot in R

This question is related to: R: how to label the x-axis of a boxplot这个问题与: R: how to label the x-axis of a boxplot

When more than one column is plotted, names appear.当绘制多于一列时,会出现名称。 But when only one column is plotted, name does not appear, even when names=.. argument is used:但是当只绘制一列时,名称不会出现,即使使用 names=.. 参数:

ddf = structure(list(apple = c(1, 2, 3, 4, 5), banana = c(5, 4, 3, 
 2, 1), watermelon = c(4, 5, 6, 7, 8)), .Names = c("apple", "banana", 
 "watermelon"), row.names = c(NA, -5L), class = "data.frame")

 ddf
  apple banana watermelon
1     1      5          4
2     2      4          5
3     3      3          6
4     4      2          7
5     5      1          8


boxplot(ddf[,1:2])
boxplot(ddf[,1])

在此处输入图片说明

在此处输入图片说明

Following also do not work:以下也不起作用:

boxplot(ddf[,1], names='apple')
boxplot(ddf[,1], names=c('apple'))

How can I add name to the boxplot when only one column is used?仅使用一列时,如何向箱线图中添加名称? Thanks for your help.谢谢你的帮助。

There is a show.names= argument to bxp , which boxplot calls.有一个show.names=参数bxp ,这boxplot电话。 You can thus do:你可以这样做:

boxplot(ddf[1], show.names=TRUE)

Make sure this is ddf[1] not ddf[,1] though, so that the name is retained.确保这是ddf[1]而不是ddf[,1] ,以便保留名称。

也许你可以使用“xlab”:

boxplot(ddf[,1], xlab="apple")

One way is to use mtext :一种方法是使用mtext

boxplot(ddf[,1])
mtext("apple", side=1, line=1)

默认情况下,箱线图在x=1添加,因此您可以将刻度和轴标签添加到x=1就像绘制多列时一样。

axis(side = 1, at = 1, labels = 'apple')

I also used the solution with show.names for Boxplot{car}.我还为show.names {car} 使用了带有show.names的解决方案。 In my case I wanted to sum up some columns in one boxplot and label the outliers at the same time, hence I used Boxplot .就我而言,我想在一个箱线图中总结一些列并同时标记异常值,因此我使用了Boxplot

Boxplot(df, show.names = T, names = "test samples", labels = rownames(df), id.method = c("y"), id.n=9)

For boxplot you don't need to support a list of names for show.names if you are satisfied with the names of your dataframe.对于boxplot ,如果您对数据show.names的名称感到满意,则不需要支持show.names的名称列表。 For Boxplot you have to supply a name for the plot.对于Boxplot您必须为绘图提供一个名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM