2

I have a data frame, df, that looks this this:

      a   b   c   d   e   f   g    h     i    j   k   l    m
1a    4   4   3   4   3   4   3   4.0   4.0   4   4   4   3.9
1b    9   9   9   9   9   9   9   8.1   8.8   9   9   9   8.5
1c    8   8   9   8   9   8   8   8.0   9.0   8   9   8   8.3
1d    8   8   8   9   8   9   8   8.0   8.0   8   8   8   8.5
1e    4   4   4   4   4   4   4   4.0   4.0   4   4   4   4.0
2a    3   4   3   4   3   4   3   4.0   3.0   4   3   4   3.8
2b    8   8   8   8   8   8   8   8.0   8.0   8   8   8   8.0
2c    8   8   8   8   8   8   8   9.0   8.0   9   8   8   8.3
2d    8   9   8   8   8   9   8   9.0   8.0   9   8   9   8.0
2e    4   3   4   3   4   4   4   4.0   4.0   4   4   3   3.9

I am using the plotrix and devtools packages, and have already installed them both, and the barp2 function like this:

# install the packages and load the barp2 function
install.packages('plotrix')
install.packages('devtools')
install_url("http://cran.r-project.org/src/contrib/Archive/plotrix/plotrix_3.5-2.tar.gz")
source_gist("https://gist.github.com/tleja/8592929")

# load the packages
library(plotrix)
library(devtools)

The modified code (barp2) that I'm using is available here.

I am trying to plot the data in the data frame, provided above, like this:

par(mar = c(5, 4, 4, 6))
barp2(df, pch=t(c(0:4, 7:14)), names.arg=rownames(df), legend.lab=colnames(df),
    ylab="y label", main="main")

I am using the reference chart, to fill the bars of the plot.

I want the rownames of df to be the x axis labels, and the colnames of df to be in a legend.

However, I keep getting this error:

Error in axis(1, at = x, labels = names.arg, cex.axis = cex.axis) : 
    'at' and 'labels' lengths differ, 13 != 10

I understand that this is because rownames(df) has a length of 10 and colnames(df) has a length of 13 (which are clearly not equal), but I am not sure how to fix this problem, so that the data in the data frame is displayed in a barplot.

Or if I swap the columns and rows around using t(df), like this:

barp2(t(df), pch=t(c(0:4, 7:11)), names.arg=rownames(df), legend.lab=colnames(df),
ylab="y label", main="main")

I get this error:

Error in seq.default(x1[frect] + xinc[frect]/2, x2[frect] - xinc[frect]/2,  : 
    wrong sign in 'by' argument

I have no idea what this error means or why I get it.

Sorry I can't provide an image of what it should look like, but hopefully you get the basic idea of it.

Any help would be much appreciated.

Warrior
  • 170
  • 3
  • 11
  • `t()` on the argument to pch just looks wrong. – IRTFM Jan 30 '14 at 02:38
  • @IShouldBuyABoat - Even if I remove the `t()` on the `pch`, I still get the same error. I had included the `t()` because _the data has to be supplied in a matrix format to specify varying symbols. The columns of the matrix should correspond with the sequence of `pch` parameters_. As someone had said on this [page](http://stackoverflow.com/questions/21323798/filling-bars-in-barplot-with-textiles-rather-than-color-in-r). – Warrior Jan 30 '14 at 03:02

0 Answers0