在特定X轴处绘制R中的平均值

时间:2016-12-05 17:08:15

标签: r mean boxplot

在R中,有没有办法为特定x轴的绘图添加平均值。例如,我想做类似的事情:

<mdl-select floating-label
      placeholder="Nazione"
      formControlName="countryid">
   <mdl-option value="0"></mdl-option>
   <mdl-option *ngFor="let country of countries" [value]="country.id">{{country.name}}</mdl-option>
</mdl-select>

我收到一条消息说“at”和“add”'不是图形参数'。有解决方法吗?

所以我使用的是RStudio。有六个不同的值(data_a,data_b,data_c,data_d,data_e和data_f),每个值有11个数字。我目前的代码如下:

plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
boxplot(dataset,at = 0.5, add = T, range = 6,yaxt = 'n')
points(mean(dataset), at = 0.5, add = T)

现在我希望能够添加平均值。

2 个答案:

答案 0 :(得分:1)

对于箱线图,x轴上的图的位置是单位(1,2,3等)。您可以使用函数/o-d检查这一点。

以下是使用虹膜数据集将方法作为红色圆圈添加到箱线图的示例:

locator()

enter image description here

答案 1 :(得分:1)

您可以尝试这个来获得所需的图(使用随机生成的data_a,data_b等):

par(xpd = FALSE)
par(mar=c(8,4.5,2,1))
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
data_a <- runif(11, 0, 300)
data_b <- runif(11, 0, 300)
data_c <- runif(11, 0, 300)
data_d <- runif(11, 0, 300)
data_e <- runif(11, 0, 300)
data_f <- runif(11, 0, 300)
boxplot(data_a,at = 0.5, add = T, range = 6,yaxt = 'n')
boxplot(data_b,at = 1.5, add = T, range = 6,yaxt = 'n')
boxplot(data_c,at = 2.5, add = T, range = 6,yaxt = 'n')
boxplot(data_d,at = 4, add = T, range = 6,yaxt = 'n')
boxplot(data_e,at = 5, add = T, range = 6,yaxt = 'n')
boxplot(data_f,at = 6, add = T, range = 6,yaxt = 'n')
axis(2, at = 150, pos = -0.65, tck = 0, labels = 'X axis label',cex.axis=1.1)
axis(1, at = c(0.5,1.5,2.5,4,5,6),labels=c('','','','','',''))
axis(1, at = c(1.5,5),pos= -25,labels=c('label 1','label 2'),tick=FALSE)
axis(1, at = c(3.25),labels=c(''),tck=-0.15)
axis(1, at = c(3.25),pos = -50,labels=c('Y axis label'),tick=FALSE)
abline(v=3.25)
par(xpd = NA)
text(0.5,-30, expression("a"))
                         text(1.5,-30, expression("b"))
                         text(2.5,-30,"c")
                         text(4,-30, expression("d"))
                         text(5,-30, expression("e"))
                         text(6,-30,"f")
points(c(0.5,1.5,2.5,4,5,6), c(mean(data_a), mean(data_b), mean(data_c), mean(data_d), mean(data_e), mean(data_f)), pch = 22, col = "darkgrey", lwd = 7)

enter image description here