Ali_A423
Ali_A423

Reputation: 61

Grouped barplot using R barplot function

I am using the following code to get grouped barplot using R barplot function.

> df <- read.csv2(text = "x;y;z
  A;40;11
  B;66;16
  C;27;13
  D;10;42")
> bp <- barplot(t(df[ , -1]), beside=T, names=df$x)

but this is not working when my data has decimals. Like I can't get a barplot if my data is like:

> df <- read.csv2(text = "x;y;z
  A;40.9;11
  B;66;16
  C;27;13
  D;10;42")

Is there a way to modify this code to get plots for the decimal values as well? I want to use R barplot function, rather than going into some R package.

Thank you

Upvotes: 0

Views: 209

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

You can use type.convert to convert the character type of data to numeric.

barplot(type.convert(t(df[-1])), beside = TRUE, names=df$x)

enter image description here

Upvotes: 0

pascal
pascal

Reputation: 1085

Use a comma rather than a dot in your df:

> df <- read.csv2(text = "x;y;z
  A;40,9;11
  B;66;16
  C;27;13
  D;10;42")

Upvotes: 1

Related Questions