wanaga3166
wanaga3166

Reputation: 35

R Barplot Chart Problem: Error in -0.01 * height : non-numeric argument to binary operator

I have a tab file .txt as the following example:

> dput(data)
structure(list(file = structure(c(1L, 3L, 2L, 4L, 5L, 6L), .Label = c("3D7B1-C22-R1", 
"3D7B1-C22-R1Nuc", "3D7B1-C22-R2", "Coh03-C26-R1", "Coh05-C22-R1", 
"Coh05-C22-R2", "Coh05-C22-R3", "Coh06-C26-R1", "Coh06-C26-R2", 
"Coh06-C26-R3", "Dec02-C26-R1", "Dec02-C26-R2", "Dec02-C26-R3", 
"Dec03-C26-R1", "Dec03-C26-R2", "Dec03-C26-R3", "Dry01-C22-R1", 
"Dry01-C22-R2", "Dry01-C22-R3", "Dry02-C22-R1", "Dry02-C22-R2", 
"Dry02-C22-R3", "Dry03-C26-R1", "Dry03-C26-R2", "Dry04-C22-R1", 
"Dry04-C22-R2", "Dry04-C22-R3", "Dry05-C22-R1", "Dry05-C22-R2", 
"PCD01-C22-R1", "PCD01-C22-R2", "PCD03-C26-R1", "PCD03-C26-R2", 
"PCD03-C26-R3", "PCD04-C22-R1", "PCD04-C22-R2", "PCD04-C22-R3", 
"Wet01-C22-R1", "Wet01-C22-R2", "Wet01-C26-R1", "Wet01-C26-R2", 
"Wet03-C22-R1", "Wet03-C22-R2", "Wet03-C22-R3", "Wet04-C22-R1"
), class = "factor"), percmapped = c(72.59865886, 69.76716768, 
87.65023774, 12.53737526, 26.34900309, 27.35084022), Percdedup = c(63.82326317, 
62.34470705, 71.51045653, 8.384373203, 20.56819736, 21.01794928
), percunique = c(61.37889243, 59.98338518, 68.20508459, 7.715037189, 
19.58090215, 20.01226738)), row.names = c(NA, 6L), class = "data.frame")

I wish to make a barplot.

So I made the following orders:

data <- read.table("Book1.txt", header = TRUE, sep = "\t", dec = ",")
head(data)

m <- t(as.matrix(data[1:4]))
colnames(m) <- data[,1]

barplot(m, main = "STAR pipeline", beside = T, ylab = "Percentage", col = colours, las = 2, cex.names = 0.6)

During the barplot, R me returns the error message below:

Error in -0.01 * height : non-numeric argument to binary operator

What does that mean? How to make my barplot?

Thanks for your help.

Upvotes: 2

Views: 721

Answers (1)

Parfait
Parfait

Reputation: 107642

Simply adjust your matrix build to keep character and numeric values separate. Then plot accordingly. Also, be sure to add the legend.text argument.

m <- t(as.matrix(data[2:4]))   # REMOVE 1
colnames(m) <- data[,1]

m    
#            3D7B1-C22-R1 3D7B1-C22-R2 3D7B1-C22-R1Nuc Coh03-C26-R1 Coh05-C22-R1 Coh05-C22-R2
# percmapped     72.59866     69.76717        87.65024    12.537375      26.3490     27.35084
# Percdedup      63.82326     62.34471        71.51046     8.384373      20.5682     21.01795
# percunique     61.37889     59.98339        68.20508     7.715037      19.5809     20.01227

barplot(m, main = "STAR pipeline", beside = T, ylab = "Percentage", col = colours, 
        las = 2, cex.names = 0.6, legend.text = row.names(m))

Using rainbow color palette and legend.text argument:

barplot(m, main = "STAR pipeline", beside = T, ylab = "Percentage", las = 2, cex.names = 0.6,
        col = rainbow(length(row.names(m))),  legend.text = row.names(m))

Bar Plot with legend.txt arg

Even more, add a ylim() for proper axis rendering and place legend on top using a separate legend() call:

barplot(m, main = "STAR pipeline", beside = T, ylab = "Percentage", las = 2, cex.names = 0.6, 
        col = rainbow(length(row.names(m))), ylim=c(0,100))

legend(x="top", legend = row.names(m), fill = rainbow(length(row.names(m))), 
       ncol=length(row.names(m)))

Bar Plot with legend() call

Rextester demo (click Run it (F8) at bottom for graphs)

Upvotes: 1

Related Questions