Search code examples
rplotscalehistogrambar-chart

R barplot horizontal, y axis missing values


R code:

vp2 <- function(symbol){
  df = data.frame(Date=index(symbol), Adjusted=symbol$Adjusted, Volume=symbol$Volume)
  dt <- as.data.table(unique(df,by=Date))
  vol <- aggregate(cbind(Volume) ~ Adjusted, sum, data=dt)
  hmatrix <- as.matrix(vol$Volume)
  par(bg=NA)
  colnamesbarplot <- c("Adjusted","Volume")
  options(scipen=50,digits=10)
  barplot(hmatrix,beside=TRUE,horiz=TRUE,axes=TRUE,legend.text=TRUE,xlab="Volume",ylab="Price")
  return(vol)
}

Snippet of sample data (df):

    Date    Adjusted    Volume
1   2013-10-29  35.41   2333100
2   2013-10-30  34.85   5929200
3   2013-10-31  34.69   5100200
4   2013-11-01  34.13   9774900
5   2013-11-04  34.04   4571600
6   2013-11-05  33.67   6565300
7   2013-11-06  34.19   9905800
8   2013-11-07  33.97   4754500
9   2013-11-07  33.97   4754500
10  2013-11-08  34.01   3722200

Resulting barplot

Problem - The issue is the y axis (price) is not showing any values.


Solution

  • You may try this:

    barplot(hmatrix, beside = TRUE, horiz = TRUE, xlab = "Volume", ylab = "")
    axis(side = 2, at = seq_along(hmatrix) + 0.5, labels = vol$Adjusted, las = 2)
    mtext(text = "Price", side = 2, line = 4)
    

    enter image description here