从tapply输出 - R脚本绘制数据

Cod*_*Guy 3 plot r

我使用tapply来获取某些值的平均值,我得到的输出看起来像:

 5        6        7        8        
 3066.892 1804.489 1754.675 1695.448
Run Code Online (Sandbox Code Playgroud)

当我绘制它时,我得到一个图,其中x轴的"索引"是0到3而不是实际值5到8.我如何绘制这个tapply输出以便轴标签是正确的?

the*_*ail 6

您应该能够使用names()函数来获取值,例如:

names(tapply(values,index,mean))
[1] "5" "6" "7" "8"

xnames <- names(tapply(values,index,mean))
Run Code Online (Sandbox Code Playgroud)

您可以创建没有x轴的绘图并添加新的数据标签

plot(tapply(values,index,mean),xaxt="n")
axis(1, at=1:length(xnames), labels=xnames)
Run Code Online (Sandbox Code Playgroud)

我认为这样做会很正常.