2

I am struggling to understand how I can get the following data into a histogram:

NSP <- c(1380, 6003, 1827, 661, 331, 156, 97, 73, 58) 
hist(NSP)

Each number should represent one bar in the exactly same order. I tried to use ggplot but failed to get the frequencies on the Y axis.

Many thanks in advance!

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89

2 Answers2

2

If you are trying to use ggplot2, then you likely want geom_col. Note that you will need to specify x-positions for the bars. Here, I am just specifying them as 1:length(NSP) using seq_along:

ggplot(
  mapping = aes(y = NSP
                , x = seq_along(NSP))) +
  geom_col() +
  scale_x_continuous(breaks = seq_along(NSP))

enter image description here

If you have other labels for the breaks, you can specify them, e.g.:

ggplot(
  mapping = aes(y = NSP
                , x = seq_along(NSP))) +
  geom_col() +
  scale_x_continuous(breaks = seq_along(NSP)
                     , labels = LETTERS[seq_along(NSP)])

enter image description here

Note, however, that ggplot generally works much more smoothly when your data are in a data.frame. This may be a more flexible approach for you, and allow more succinct naming of the column locations (e.g., if they are not evenly spaced, you could have one column for the actual location, and one column for the label).

NSP_df <- data.frame(Location = LETTERS[seq_along(NSP)]
                     , Count = NSP)

ggplot(
  NSP_df
  , aes(y = Count
        , x = Location)) +
  geom_col()

enter image description here

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • Thanks a lot! I will try this and let you know if it works out! –  Jan 04 '17 at 14:06
  • But how do I describe the A, B, C, D and give them names? Do you happen to know that, too? Many thanks! –  Jan 07 '17 at 17:34
  • The exact same way I added the letters. Just replace `LETTERS[seq_along(NSP)]` with whatever labels you want to use. – Mark Peterson Jan 07 '17 at 17:59
  • Dear Mark, one last question, how can I name the axses a? I only know: xlab="NSP position - intervals", ylab="Number of transactions", main="NSP Distribution of Publicly Disclosed Transactions" –  Apr 10 '17 at 14:32
  • As lables, I wanted to use the following code: ggplot( mapping = aes(y = NSP_Histo , x = seq_along(NSP_Histo))) + geom_col() + scale_x_continuous(breaks = seq_along(NSP_Histo) , labels = "<0.5", "0.5-1.0", "1.0-1.5","1.5-2.0", "2.0-2.5", "2.5-3.0", "3.0-3.5", "3.5-4.0", ">4.0") - But this seems not to work –  Apr 10 '17 at 14:36
  • Axis labels can be added with `xlab()` and `ylab()`. The main title can be added with `ggtitle`. What do you mean by "not to work"? I do notice that you are missing a `c()` around your labels -- right now only "<0.5" is passed to `labels = `. The rest are being passed in as unnamed arguments to the `scale` function. – Mark Peterson Apr 12 '17 at 12:29
1

Maybe what you want is barplot(NSP)?

R plot output

Thales
  • 585
  • 3
  • 9