带有基本R函数的多因子标签的boxplot(boxplot with multiple factor labels using base R functions)

编程入门 行业动态 更新时间:2024-10-28 01:13:30
带有基本R函数的多因子标签的boxplot(boxplot with multiple factor labels using base R functions)

如何才能重现本答案中显示ggplot-based boxplot图,但使用base R boxplot function ?

上述链接的样本日期:

d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500)) # first factor has 20+ levels d$f1<-factor(d$f1) # second factor a,b,c d$f2<-factor(d$f2) boxplot(x~f2*f1,data=d,col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)

如果x-axis的组彼此间隔开,那将是很好的。

我对ggplot2了解ggplot2 。

编辑

在使用基本R函数等待更多建议的同时,我正在使用ggplot2取得一些进展。

使用此样本数据,如何生成具有良好对齐的x-axis如上面链接中的那个?

以下内容没有给出正确的对齐方式(我希望数字1:8在每个组的中心对齐):

library(ggplot2) ggplot(dat3, aes(x = ID, y = value, group=interaction(obs, ID), fill=obs)) + geom_boxplot() + scale_fill_manual(values = c("yellow", "orange")) dat3=structure(list(values = c(0, 0, 0, 0, 0, 0, 0, 0, -0.0169491525423729, 0, 0, 0, 0, 1, 1, 0.64367816091954, 0.64367816091954, 0, 0, -0.0163934426229508, -0.021978021978022, 0.109195402298851, 0, 0, 0, 0, 0.207650273224044, 0.4375, 0, 0, 0, 0, 0.302325581395349, 0.303370786516854, 0.270588235294118, -0.0188679245283019, 0.156462585034014, 0.092436974789916, 0.69, -0.021978021978022, 0.64367816091954, 0.614906832298137, 0.612903225806452, 0.274853801169591, 0, 0.303370786516854, 0, 0, -0.03125, 0.229813664596273, 0.557142857142857, 0, 0.109195402298851, 0.0746268656716418, 0.180616740088106, 0.210526315789474, 0.310344827586207, 1, 1, 0.0825688073394495, 0.294117647058824, 0, 0.4375, 0, 0.230769230769231, 0.347826086956522, -0.0163934426229508, 0.156462585034014, 0, 0, 0, 1, 0, 0, 0, 0.483333333333333, 0.483333333333333, 0, 0, 0, 0, 0, -0.0169491525423729, 0, 0.310344827586207, 0, 0.296875, 0.302325581395349, 0, 0, 0, 0, 0, 0, 0.482758620689655, 0, 0, 0, 0, 0, 0, 0, 0, 0.150684931506849, 0.150684931506849, 0, 0, -0.021978021978022, -0.021978021978022, 0.270588235294118, 0, 0, 0.482758620689655, 0.482758620689655, 0.272727272727273, 0.272727272727273, 0, 1, 0, 0, 0.642857142857143, 0.211864406779661, 0.156462585034014, -0.0449438202247191, -0.0449438202247191, 0.389763779527559, 0.389763779527559, -0.021978021978022, 0.211864406779661, 0.213197969543147, 0.213197969543147, 0.358620689655172, -0.0163934426229508, 0.483333333333333, 0, 0, 0.362139917695473, 0.362139917695473, 0.261904761904762, 0.483333333333333, 1, 1, 0.236453201970443, 0.302325581395349, 0.310344827586207, 1, 1, 0.358974358974359, 0.358974358974359, -0.0606060606060606, 0.0721649484536082, 0.615384615384615, 0.615384615384615, 0.347826086956522, 1, 0, 0, 0, -0.0273972602739726, -0.0273972602739726, -0.0169491525423729, -0.0256410256410256, 0.107142857142857, 0.107142857142857, 0.302325581395349, -0.0163934426229508, -0.0264900662251656, 0.311111111111111, 0.311111111111111, 0.156462585034014, 0.156462585034014, -0.0483091787439614, 0.311111111111111, -0.0333333333333333, -0.0333333333333333, 0.311111111111111), ind = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 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L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("4", "5", "6", "7", "8", "9", "10", "11"), class = "factor"), obs = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("obs", "capa"), class = "factor")), .Names = c("values", "ind", "ID", "obs"), row.names = c(NA, 176L), class = "data.frame")

How can one possibly reproduce the ggplot-based boxplot shown in this answer but using base R boxplot function?

Sample date from the above link:

d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500)) # first factor has 20+ levels d$f1<-factor(d$f1) # second factor a,b,c d$f2<-factor(d$f2) boxplot(x~f2*f1,data=d,col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)

It would be great if the groups on the x-axis are spaced from each other.

I have limited knowledge about ggplot2.

EDIT

While waiting for more suggestions using base R functions, I am making some progress with ggplot2.

Using this sample data how can I produce a plot with well aligned x-axis as the one in the link above?

The following does not give me the correct alignment (I want the numbers 1:8 aligned at the center of each group):

library(ggplot2) ggplot(dat3, aes(x = ID, y = value, group=interaction(obs, ID), fill=obs)) + geom_boxplot() + scale_fill_manual(values = c("yellow", "orange")) dat3=structure(list(values = c(0, 0, 0, 0, 0, 0, 0, 0, -0.0169491525423729, 0, 0, 0, 0, 1, 1, 0.64367816091954, 0.64367816091954, 0, 0, -0.0163934426229508, -0.021978021978022, 0.109195402298851, 0, 0, 0, 0, 0.207650273224044, 0.4375, 0, 0, 0, 0, 0.302325581395349, 0.303370786516854, 0.270588235294118, -0.0188679245283019, 0.156462585034014, 0.092436974789916, 0.69, -0.021978021978022, 0.64367816091954, 0.614906832298137, 0.612903225806452, 0.274853801169591, 0, 0.303370786516854, 0, 0, -0.03125, 0.229813664596273, 0.557142857142857, 0, 0.109195402298851, 0.0746268656716418, 0.180616740088106, 0.210526315789474, 0.310344827586207, 1, 1, 0.0825688073394495, 0.294117647058824, 0, 0.4375, 0, 0.230769230769231, 0.347826086956522, -0.0163934426229508, 0.156462585034014, 0, 0, 0, 1, 0, 0, 0, 0.483333333333333, 0.483333333333333, 0, 0, 0, 0, 0, -0.0169491525423729, 0, 0.310344827586207, 0, 0.296875, 0.302325581395349, 0, 0, 0, 0, 0, 0, 0.482758620689655, 0, 0, 0, 0, 0, 0, 0, 0, 0.150684931506849, 0.150684931506849, 0, 0, -0.021978021978022, -0.021978021978022, 0.270588235294118, 0, 0, 0.482758620689655, 0.482758620689655, 0.272727272727273, 0.272727272727273, 0, 1, 0, 0, 0.642857142857143, 0.211864406779661, 0.156462585034014, -0.0449438202247191, -0.0449438202247191, 0.389763779527559, 0.389763779527559, -0.021978021978022, 0.211864406779661, 0.213197969543147, 0.213197969543147, 0.358620689655172, -0.0163934426229508, 0.483333333333333, 0, 0, 0.362139917695473, 0.362139917695473, 0.261904761904762, 0.483333333333333, 1, 1, 0.236453201970443, 0.302325581395349, 0.310344827586207, 1, 1, 0.358974358974359, 0.358974358974359, -0.0606060606060606, 0.0721649484536082, 0.615384615384615, 0.615384615384615, 0.347826086956522, 1, 0, 0, 0, -0.0273972602739726, -0.0273972602739726, -0.0169491525423729, -0.0256410256410256, 0.107142857142857, 0.107142857142857, 0.302325581395349, -0.0163934426229508, -0.0264900662251656, 0.311111111111111, 0.311111111111111, 0.156462585034014, 0.156462585034014, -0.0483091787439614, 0.311111111111111, -0.0333333333333333, -0.0333333333333333, 0.311111111111111), ind = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L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 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("obs", "capa"), class = "factor")), .Names = c("values", "ind", "ID", "obs"), row.names = c(NA, 176L), class = "data.frame")

最满意答案

您可以使用选项指定框的位置。

set.seed(1) d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500)) # first factor has 20+ levels d$f1<-factor(d$f1) # second factor a,b,c d$f2<-factor(d$f2) boxplot(x~f2*f1,data=d, at = (1:80)[-4*(1:20)], col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE) axis(1,at=seq(2,80,4),labels=1:20,cex.axis=0.7)

在此处输入图像描述

You can specify the location of the boxes using at option.

set.seed(1) d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500)) # first factor has 20+ levels d$f1<-factor(d$f1) # second factor a,b,c d$f2<-factor(d$f2) boxplot(x~f2*f1,data=d, at = (1:80)[-4*(1:20)], col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE) axis(1,at=seq(2,80,4),labels=1:20,cex.axis=0.7)

enter image description here

更多推荐

本文发布于:2023-08-02 03:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1369869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:因子   函数   标签   boxplot   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!