2016-12-05 79 views
0

在R中,是否有一種方法可以在特定的x軸處爲繪圖添加平均值。例如,我想要做的事,如:「在」在特定的X軸處繪製R中的平均值

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) 

我得到一個消息,說和「添加」是「不是一個圖形化的參數」。有沒有解決方法?

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

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") 
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','labe 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") 

現在我希望能夠加上意思。

+0

可以添加您的SAM ple數據集 –

回答

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

1

對於箱形圖,x軸上的圖的位置以單位(1,2,3等)爲單位。您可以使用功能locator()進行檢查。

這裏是作爲一個紅圈加入裝置,使用虹膜數據集的箱線圖爲例:

boxplot(Sepal.Length ~ Species, data = iris) 
points(seq_along(levels(iris$Species)), with(iris, tapply(Sepal.Length, Species, mean)), col = "red") 

enter image description here

相關問題