如何在boxplot上放置值并控制其宽度?
X<-c(1,2,,3,4,4,5,5,6,6,6,6,6,7)
Run Code Online (Sandbox Code Playgroud)
我需要为min,max,1st quartile,median和last quartile写入值.我该怎么把它放在那里?
mne*_*nel 17
您可以使用horizontal = TRUE获取水平箱图并axes = FALSE删除轴.staplewex = 1将订书钉宽度设置为与框宽度相同
然后你可以fivenum用来返回用于创建箱线图的统计数据,并将它们用作文本标签,摆弄y价值直到你拥有你想要的东西
boxplot(X, horizontal = TRUE, axes = FALSE, staplewex = 1)
text(x=fivenum(X), labels =fivenum(X), y=1.25)
Run Code Online (Sandbox Code Playgroud)

请注意,我3为示例数据中缺少的值插入了一个X
mpa*_*nco 10
mnel提供的答案是完美的,假设 - 在示例中 - 没有异常值.在那种情况下,下部和上部晶须等于最小值和最大值.但是,如果存在异常值且我们未range = 0在boxplot函数中指定,则我们无法获得标签的正确值.相反,我们可以使用:
text(x=boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)
Run Code Online (Sandbox Code Playgroud)
我们来看一个例子:
水平
X <- c(1,2,3,3,4,4,5,5,6,6,6,6,10,15)
boxplot(X, horizontal = TRUE, axes = FALSE, staplewex = 1)
Run Code Online (Sandbox Code Playgroud)
text(x = fivenum(X), labels = fivenum(X), y = 1.25) 
text(x = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)
垂直
切换x和y内部的参数text.
boxplot(X, axes = FALSE, staplewex = 1)
text(y = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, x = 1.25)
Run Code Online (Sandbox Code Playgroud)