Tarek Khedr
Tarek Khedr

Reputation: 35

How to plot a multiple columns for one row using R

I have a data set aus_noscl contains 1 row and multiple columns, for example :

Country      x1999      x2000      x2001      x2002
Australia    27,5444    26,200     27,243     15,685

I want to plot a histogram to compare years,

I have tried :

  barplot(cbind(aus_noscl$X2010, aus_noscl$X2011, aus_noscl$X2012,
          aus_noscl$X2013))

But: It will take a long time writing all columns names and in the graph no name for columns appear, so it's not valuable visualization.

Would be very grateful for easy and effective idea for this kind of data, thank you

Upvotes: 1

Views: 302

Answers (1)

akrun
akrun

Reputation: 886938

We can unlist the dataset excluding the first column, remove the , using gsub, convert to numeric class, set the names of the vector by the column names and then do the barplot.

barplot(setNames(as.numeric(gsub(',', '', 
          unlist(aus_nosci[-1]))), names(aus_nosci[-1])))

Upvotes: 1

Related Questions