使用R中的直方图可视化数据

时间:2014-05-20 08:51:20

标签: r histogram

我正在尝试将一些数据可视化,为了做到这一点,我正在使用R' hist

贝娄是我的数据

jancoefabs <- as.numeric(as.vector(abs(Janmodelnorm$coef)))
jancoefabs

[1] 1.165610e+00 1.277929e-01 4.349831e-01 3.602961e-01 7.189458e+00
 [6] 1.856908e-04 1.352052e-05 4.811291e-05 1.055744e-02 2.756525e-04
[11] 2.202706e-01 4.199914e-02 4.684091e-02 8.634340e-01 2.479175e-02
[16] 2.409628e-01 5.459076e-03 9.892580e-03 5.378456e-02

现在你可能已经猜到了这些是一些模型系数的绝对值。

我需要的是一个用于轴的直方图:

x将是系数的总数(countlength),总数为19,以及它们的名称。

y会根据这些值的breaksylim=""显示设置min的每列的值(max?)(或Janmodelnorm$coef?类似的东西)。

请注意, (Intercept) LON LAT ME RAT 1.165610e+00 -1.277929e-01 -4.349831e-01 -3.602961e-01 -7.189458e+00 DS DSA DSI DRNS DREW -1.856908e-04 1.352052e-05 4.811291e-05 -1.055744e-02 -2.756525e-04 ASPNS ASPEW SI CUR W_180_270 -2.202706e-01 -4.199914e-02 4.684091e-02 -8.634340e-01 -2.479175e-02 W_0_360 W_90_180 W_0_180 NDVI 2.409628e-01 5.459076e-03 -9.892580e-03 -5.378456e-02 只会生成以下内容

?hist

到目前为止,在咨询# hist(jancoefabs, col="lightblue", border="pink", # breaks=8, # xlim=c(0,10), ylim=c(20,-20), plot=TRUE) 时,我试图使用下面的代码而没有成功。因此,我从头开始接受它。

plot=FALSE

设置breaks后,我会得到一些有关该集的有用信息。我也发现很难有效地使用{{1}}参数。

任何建议将不胜感激。感谢。

2 个答案:

答案 0 :(得分:2)

为什么不使用hist或标准barplot,而不是使用plot。例如,

## Generate some data
set.seed(1)
y = rnorm(19, sd=5)
names(y) = c("Inter", LETTERS[1:18])

然后绘制cofficients

barplot(y)

或者,您可以使用散点图

plot(1:19, y, axes=FALSE, ylim=c(-10, 10))
axis(2)
axis(1, 1:19, names(y))

并添加错误栏以指示标准错误(请参阅例如Add error bars to show standard deviation on a plot in R

答案 1 :(得分:1)

您确定需要直方图吗?格子barchart可能非常好。 mtcars内置数据集的示例。

> coef <- lm(mpg ~ ., data = mtcars)$coef
> library(lattice)
> barchart(coef, col = 'lightblue', horizontal = FALSE, 
          ylim = range(coef), xlab = '', 
          scales = list(y = list(labels = coef),
                        x = list(labels = names(coef))))

enter image description here

基础R dotchart可能也不错,

> dotchart(coef, pch = 19, xlab = 'value')
> text(coef, seq(coef), labels = round(coef, 3), pos = 2)

enter image description here