0

I have the following simple dataset (a small section shown below) and want to produce a histogram from this using R studio (I am able to do this using Excel).

Samples   Number.of.OTUs
      A               13
      B               10
      C                9
      D                9

The dataset has been read in using the following command;

MD1 = read.csv("MD_qual_OTU_sorted_2.csv")

when I try to make a histogram I get this error

hist(MD1)

Error in hist.default(MD1) : 'x' must be numeric

and this when I try barplot

barplot(MD1)

Error in barplot.default(MD1) : 'height' must be a vector or a matrix

attributes(MD1)
# $names
# [1] "Samples"        "Number.of.OTUs"

# $class
# [1] "data.frame"

# $row.names
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18

I know I could enter the data by hand in R but I want to be able to read in the CSV file preferably.

I have tried the solutions in the question marked as duplicate, but to no avail.

  • Side note: `barplot` and `hist` are totally different things. –  Nov 02 '15 at 09:53
  • Possible duplicate, e.g.: http://stackoverflow.com/questions/2349205/cant-draw-histogram-x-must-be-numeric – symbolrush Nov 02 '15 at 09:54
  • Thanks for that link Adrian, I did previously try MD1= read.csv("MD_qual_OTU_sorted_2.csv", header= T, sep=",") and MD1= read.csv("MD_qual_OTU_sorted_2.csv", header= T, dec=",") but with no joy :( – user2966451 Nov 02 '15 at 10:05

1 Answers1

2

Given the data I guess what you want is a barplot. The error you get is the result of a misspecification in the barplot command. The first argument (height) should be the Number.of.OTUs in your case, and you can specify the labels of the bars in the names.arg argument. What you're probably looking for is something like this:

MD1<-data.frame(Samples=c("A","B","C","D"), Number.of.OTUs=c(13,10,9,9))
par(las=1)  
barplot(MD1[,2],names.arg=MD1[,1])

enter image description here

horseoftheyear
  • 917
  • 11
  • 23