0

I am trying to visualize some data and in order to do it I am using R's hist.

Bellow are my data

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

Now as the more cunning of you might have guessed these are the absolute values of some model's coefficients.

What I need is an histogram that will have for axes:

x will be the number (count or length) of coefficients which is 19 in total, along with their names.

y will show values of each column (as breaks?) having a ylim="" set, according to min and max of those values (or something similar).

Note that Janmodelnorm$coef simply produces the following

  (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

So far and consulting ?hist, I am trying to play with the code bellow without success. Therefore I am taking it from scratch.

# hist(jancoefabs, col="lightblue", border="pink",
     # breaks=8,
     # xlim=c(0,10),  ylim=c(20,-20), plot=TRUE)

When plot=FALSE is set, I get a bunch of somewhat useful info about the set. I also find hard to use breaks argument efficiently.

Any suggestion will be appreciated. Thanks.

  • Are you trying to make histogram for 19 values? Maybe try barplot? – zx8754 May 20 '14 at 08:58
  • @zx8754 i think its clear i am trying to make histogram for `Janmodelnorm$coef` which has 19 values so the obvious answer would be yes.. –  May 20 '14 at 08:59
  • 1
    When we have large numbers, hist is useful, otherwise barplot gives better info. – zx8754 May 20 '14 at 09:01
  • 2
    A histogram will almost always have fewer bars than data values. The point of a histogram is to visualize how many data values fall into each of a set of intervals. What you seem to be trying to create is a barplot, which is something different. I suggest that you revisit the definitions of histograms and barplots, e.g., at Wikipedia. – Stephan Kolassa May 20 '14 at 09:03
  • @StephanKolassa Apparently I should do that –  May 20 '14 at 09:04

2 Answers2

2

Rather than using hist, why not use a barplot or a standard plot. For example,

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

Then plot the cofficients

barplot(y)

Alternatively, you could use a scatter plot

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

and add error bars to indicate the standard errors (see for example Add error bars to show standard deviation on a plot in R)

Community
  • 1
  • 1
csgillespie
  • 59,189
  • 14
  • 150
  • 185
1

Are you sure you want a histogram for this? A lattice barchart might be pretty nice. An example with the mtcars built-in data set.

> 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

A base R dotchart might be good too,

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

enter image description here

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245